adding zeroes to a string variable

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

adding zeroes to a string variable

vstirkey
have you ever had a syntax that works, then it doesn't? I have a variable that has values that are 3-5 digits. I want them all to be 5 digits, by adding 0 to the end of those that are only 3 or 4 digits long. The syntax worked last year when I had to do this, this year it didn't work at first, but then it did for one variable- I tried to run the next one, and again it didn't work. I can't see where there are any differences in these 2 variables. Can anyone suggest another way to do this? And why would something work one time and then not work another time? Here is the syntax I used.  

STRING Diag1 (A5).
COMPUTE Diag1 = char.RPAD(ltrim(DiagnosisCode1),5,"0") .
EXECUTE .

STRING Diag2 (A5).
COMPUTE Diag2 = char.RPAD(ltrim(DiagnosisCode2),5,"0") .
EXECUTE .

STRING Diag3 (A5).
COMPUTE Diag3 = char.RPAD(ltrim(DiagnosisCode3),5,"0") .
EXECUTE .

my data looks like this-- result should be this

29500                           29500
311                              31100
3004                            30040
29750                          29750
29750                          29750

Thanks in advance for any guidance you can give!
Reply | Threaded
Open this post in threaded view
|

Re: adding zeroes to a string variable

David Marso
Administrator
You should always post any error messages!
Also, 'doesn't work' is really not very helpful!
What do you mean by 'doesn't work'?
Meanwhile confirm that each of the original variables are defined as string and NOT numeric.

"And why would something work one time and then not work another time?"
Answer: Differing context? Something IS DIFFERENT?

vstirkey wrote
have you ever had a syntax that works, then it doesn't? I have a variable that has values that are 3-5 digits. I want them all to be 5 digits, by adding 0 to the end of those that are only 3 or 4 digits long. The syntax worked last year when I had to do this, this year it didn't work at first, but then it did for one variable- I tried to run the next one, and again it didn't work. I can't see where there are any differences in these 2 variables. Can anyone suggest another way to do this? And why would something work one time and then not work another time? Here is the syntax I used.  

STRING Diag1 (A5).
COMPUTE Diag1 = char.RPAD(ltrim(DiagnosisCode1),5,"0") .
EXECUTE .

STRING Diag2 (A5).
COMPUTE Diag2 = char.RPAD(ltrim(DiagnosisCode2),5,"0") .
EXECUTE .

STRING Diag3 (A5).
COMPUTE Diag3 = char.RPAD(ltrim(DiagnosisCode3),5,"0") .
EXECUTE .

my data looks like this-- result should be this

29500                           29500
311                              31100
3004                            30040
29750                          29750
29750                          29750

Thanks in advance for any guidance you can give!
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: adding zeroes to a string variable

Andy W
In reply to this post by vstirkey
My best would be that DiagnosisCode needs to be a string for your code to work, and in the new dataset it is now in a numeric variable. Possible solutions are to use ALTER TYPE to change DiagnosisCode to a string variable, or instead of

LTRIM(DiagnosisCode1)

use

LTRIM(STRING(DiagnosisCode1,F5.0))

To make consistent code though all three of the DiagnosisCode variables need to be in the same string or numeric format.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

RE: adding zeroes to a string variable

vstirkey
In reply to this post by David Marso

Thanks for the response! There is no error message,

The resulting variable is exactly the same as the original, like so.

 

29500                         29500
311                              311  
3004                            3004  
29750                          29750
29750                          29750

 

From: David Marso [via SPSSX Discussion] [mailto:[hidden email]]
Sent: Wednesday, December 24, 2014 12:33 PM
To: Stirkey, Vicki
Subject: Re: adding zeroes to a string variable

 

You should always post any error messages!
Also, 'doesn't work' is really not very helpful!
What do you mean by 'doesn't work'?
Meanwhile confirm that each of the original variables are defined as string and NOT numeric.

"And why would something work one time and then not work another time?"
Answer: Differing context? Something IS DIFFERENT?

vstirkey wrote

have you ever had a syntax that works, then it doesn't? I have a variable that has values that are 3-5 digits. I want them all to be 5 digits, by adding 0 to the end of those that are only 3 or 4 digits long. The syntax worked last year when I had to do this, this year it didn't work at first, but then it did for one variable- I tried to run the next one, and again it didn't work. I can't see where there are any differences in these 2 variables. Can anyone suggest another way to do this? And why would something work one time and then not work another time? Here is the syntax I used.  

STRING Diag1 (A5).
COMPUTE Diag1 = char.RPAD(ltrim(DiagnosisCode1),5,"0") .
EXECUTE .

STRING Diag2 (A5).
COMPUTE Diag2 = char.RPAD(ltrim(DiagnosisCode2),5,"0") .
EXECUTE .

STRING Diag3 (A5).
COMPUTE Diag3 = char.RPAD(ltrim(DiagnosisCode3),5,"0") .
EXECUTE .

my data looks like this-- result should be this

29500                           29500
311                              31100
3004                            30040
29750                          29750
29750                          29750

Thanks in advance for any guidance you can give!

Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"

 


If you reply to this email, your message will be added to the discussion below:

http://spssx-discussion.1045642.n5.nabble.com/adding-zeroes-to-a-string-variable-tp5728279p5728280.html

To unsubscribe from adding zeroes to a string variable, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

RE: adding zeroes to a string variable

Bruce Weaver
Administrator
Re David's other point, what types of variables (i.e., numeric or string) are the input and output variables?  


vstirkey wrote
Thanks for the response! There is no error message,
The resulting variable is exactly the same as the original, like so.

29500                         29500
311                              311
3004                            3004
29750                          29750
29750                          29750


From: David Marso [via SPSSX Discussion] [mailto:[hidden email]]
Sent: Wednesday, December 24, 2014 12:33 PM
To: Stirkey, Vicki
Subject: Re: adding zeroes to a string variable

You should always post any error messages!
Also, 'doesn't work' is really not very helpful!
What do you mean by 'doesn't work'?
Meanwhile confirm that each of the original variables are defined as string and NOT numeric.

"And why would something work one time and then not work another time?"
Answer: Differing context? Something IS DIFFERENT?
vstirkey wrote
have you ever had a syntax that works, then it doesn't? I have a variable that has values that are 3-5 digits. I want them all to be 5 digits, by adding 0 to the end of those that are only 3 or 4 digits long. The syntax worked last year when I had to do this, this year it didn't work at first, but then it did for one variable- I tried to run the next one, and again it didn't work. I can't see where there are any differences in these 2 variables. Can anyone suggest another way to do this? And why would something work one time and then not work another time? Here is the syntax I used.

STRING Diag1 (A5).
COMPUTE Diag1 = char.RPAD(ltrim(DiagnosisCode1),5,"0") .
EXECUTE .

STRING Diag2 (A5).
COMPUTE Diag2 = char.RPAD(ltrim(DiagnosisCode2),5,"0") .
EXECUTE .

STRING Diag3 (A5).
COMPUTE Diag3 = char.RPAD(ltrim(DiagnosisCode3),5,"0") .
EXECUTE .

my data looks like this-- result should be this

29500                           29500
311                              31100
3004                            30040
29750                          29750
29750                          29750

Thanks in advance for any guidance you can give!
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"

________________________________
If you reply to this email, your message will be added to the discussion below:
http://spssx-discussion.1045642.n5.nabble.com/adding-zeroes-to-a-string-variable-tp5728279p5728280.html
To unsubscribe from adding zeroes to a string variable, click here<http://spssx-discussion.1045642.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5728279&code=dnN0aXJrZXlAcGEuZ292fDU3MjgyNzl8LTk2ODg0NTY2>.
NAML<http://spssx-discussion.1045642.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

RE: adding zeroes to a string variable

vstirkey

Both input and output are strings.

 

From: Bruce Weaver [via SPSSX Discussion] [mailto:[hidden email]]
Sent: Wednesday, December 24, 2014 12:53 PM
To: Stirkey, Vicki
Subject: RE: adding zeroes to a string variable

 

Re David's other point, what types of variables (i.e., numeric or string) are the input and output variables?  

vstirkey wrote

Thanks for the response! There is no error message,
The resulting variable is exactly the same as the original, like so.

29500                         29500
311                              311
3004                            3004
29750                          29750
29750                          29750


From: David Marso [via SPSSX Discussion] [mailto:[hidden email]]
Sent: Wednesday, December 24, 2014 12:33 PM
To: Stirkey, Vicki
Subject: Re: adding zeroes to a string variable

You should always post any error messages!
Also, 'doesn't work' is really not very helpful!
What do you mean by 'doesn't work'?
Meanwhile confirm that each of the original variables are defined as string and NOT numeric.

"And why would something work one time and then not work another time?"
Answer: Differing context? Something IS DIFFERENT?
vstirkey wrote
have you ever had a syntax that works, then it doesn't? I have a variable that has values that are 3-5 digits. I want them all to be 5 digits, by adding 0 to the end of those that are only 3 or 4 digits long. The syntax worked last year when I had to do this, this year it didn't work at first, but then it did for one variable- I tried to run the next one, and again it didn't work. I can't see where there are any differences in these 2 variables. Can anyone suggest another way to do this? And why would something work one time and then not work another time? Here is the syntax I used.

STRING Diag1 (A5).
COMPUTE Diag1 = char.RPAD(ltrim(DiagnosisCode1),5,"0") .
EXECUTE .

STRING Diag2 (A5).
COMPUTE Diag2 = char.RPAD(ltrim(DiagnosisCode2),5,"0") .
EXECUTE .

STRING Diag3 (A5).
COMPUTE Diag3 = char.RPAD(ltrim(DiagnosisCode3),5,"0") .
EXECUTE .

my data looks like this-- result should be this

29500                           29500
311                              31100
3004                            30040
29750                          29750
29750                          29750

Thanks in advance for any guidance you can give!
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"

________________________________
If you reply to this email, your message will be added to the discussion below:
http://spssx-discussion.1045642.n5.nabble.com/adding-zeroes-to-a-string-variable-tp5728279p5728280.html
To unsubscribe from adding zeroes to a string variable, click here<
NAML<
http://spssx-discussion.1045642.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>

--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.

 


If you reply to this email, your message will be added to the discussion below:

http://spssx-discussion.1045642.n5.nabble.com/adding-zeroes-to-a-string-variable-tp5728279p5728283.html

To unsubscribe from adding zeroes to a string variable, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: adding zeroes to a string variable

Jon K Peck
Since the strings appear not to have any leading blanks, the ltrim does nothing, but if you are not in Unicode mode, you would need an rtrim.  In Unicode mode, trailing blanks are automatically removed.

Also, might you have nonprinting characters at the end of these strings for some of the variables?  You can change the format to AHEX10 to see the  numerical codes.  A blank should appear as 20 in AHEX.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        vstirkey <[hidden email]>
To:        [hidden email]
Date:        12/24/2014 10:54 AM
Subject:        Re: [SPSSX-L] adding zeroes to a string variable
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Both input and output are strings.

 

From: Bruce Weaver [via SPSSX Discussion] [mailto:[hidden email]]
Sent:
Wednesday, December 24, 2014 12:53 PM
To:
Stirkey, Vicki
Subject:
RE: adding zeroes to a string variable

 

Re David's other point, what types of variables (i.e., numeric or string) are the input and output variables?  

vstirkey wrote

Thanks for the response! There is no error message,
The resulting variable is exactly the same as the original, like so.

29500                         29500
311                              311
3004                            3004
29750                          29750
29750                          29750


From: David Marso [via SPSSX Discussion] [mailto:
[hidden email]]
Sent: Wednesday, December 24, 2014 12:33 PM
To: Stirkey, Vicki
Subject: Re: adding zeroes to a string variable

You should always post any error messages!
Also, 'doesn't work' is really not very helpful!
What do you mean by 'doesn't work'?
Meanwhile confirm that each of the original variables are defined as string and NOT numeric.

"And why would something work one time and then not work another time?"
Answer: Differing context? Something IS DIFFERENT?
vstirkey wrote
have you ever had a syntax that works, then it doesn't? I have a variable that has values that are 3-5 digits. I want them all to be 5 digits, by adding 0 to the end of those that are only 3 or 4 digits long. The syntax worked last year when I had to do this, this year it didn't work at first, but then it did for one variable- I tried to run the next one, and again it didn't work. I can't see where there are any differences in these 2 variables. Can anyone suggest another way to do this? And why would something work one time and then not work another time? Here is the syntax I used.

STRING Diag1 (A5).
COMPUTE Diag1 = char.RPAD(ltrim(DiagnosisCode1),5,"0") .
EXECUTE .

STRING Diag2 (A5).
COMPUTE Diag2 = char.RPAD(ltrim(DiagnosisCode2),5,"0") .
EXECUTE .

STRING Diag3 (A5).
COMPUTE Diag3 = char.RPAD(ltrim(DiagnosisCode3),5,"0") .
EXECUTE .

my data looks like this-- result should be this

29500                           29500
311                              31100
3004                            30040
29750                          29750
29750                          29750

Thanks in advance for any guidance you can give!
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"

________________________________
If you reply to this email, your message will be added to the discussion below:

http://spssx-discussion.1045642.n5.nabble.com/adding-zeroes-to-a-string-variable-tp5728279p5728280.html
To unsubscribe from adding zeroes to a string variable, click here<

NAML<
http://spssx-discussion.1045642.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
--
Bruce Weaver

[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.

 


If you reply to this email, your message will be added to the discussion below:

http://spssx-discussion.1045642.n5.nabble.com/adding-zeroes-to-a-string-variable-tp5728279p5728283.html
To unsubscribe from adding zeroes to a string variable, click here.
NAML



View this message in context: RE: adding zeroes to a string variable
Sent from the
SPSSX Discussion mailing list archive at Nabble.com.
===================== To manage your subscription to SPSSX-L, send a message to
LISTSERV@... (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD