Help using Base SAS procedures

Padding of character variable with zeroes.

Reply
Occasional Contributor
Posts: 6

Padding of character variable with zeroes.

I have a question. My data has following values:

 

zip

123456789

-123456789

1234

-.234

12345

-.23456789

12345

12345

1234

123456789

 

I want to get a single variable with:

  • first 6 digit from 9 digit zip var;
  • add leading zero where there is 1 less  digit zip and
  • keep 6 digit zip value as it is.
  • I tried this but it doesn't give me expected results. So I tried increasing the zw.d format to 7.3 and its working for values with negative sign but the other values are not correct. Expected format is 6.3.
     padded = put(input(charvar,best.),zw.d);

Thank you 

Super User
Posts: 20,716

Re: Padding of character variable with zeroes.

Posted in reply to Aidaan_10

You need to replace w and d with numeric values. 

It looks like you want Z5. format.

 


  • add leading zero where there is 1 less  digit zip and

It's not clear what that means...

It would likely help if you showed what you want as output from this data.

 


Aidaan_10 wrote:

I have a question. My data has following values:

 

zip

123456789

-123456789

1234

-.234

12345

-.23456789

12345

12345

1234

123456789

 

I want to get a single variable with:

  • first 6 digit from 9 digit zip var;
  • add leading zero where there is 1 less  digit zip and
  • keep 6 digit zip value as it is.
  • I tried this but it doesn't give me expected results. So I tried increasing the zw.d format to 7.3 and its working for values with negative sign but the other values are not correct. Expected format is 6.3.
     padded = put(input(charvar,best.),zw.d);

Thank you 


 

Occasional Contributor
Posts: 6

Re: Padding of character variable with zeroes.

I have some mixed values in the character variable for example,  0.155322 and -0.155322

So however in my output I want the value to be displayed as 0.1553 and  -0.1553 .

 

I used want=put(input(charvar,best.),z7.4);

To accomodate the negative sign I used 7.4 format but this is not what I want... I want just 6.4 format assigned on my values...so if I use 6.4 format my values are displayed as -.1553. thanks

Super Contributor
Posts: 311

Re: Padding of character variable with zeroes.

Posted in reply to Aidaan_10
Please give the full list of values in the final desired datastep. It is still unclear exactly what you mean, at least to me, particularly in relation to - and . as part of the values. (It's also confusing here you use "zip" as the variable name - in the US, that would be "zip code" which is a common field that _is_ zero padded, but wouldn't have negative or decimal, so perhaps this is part of the confusion.)
Occasional Contributor
Posts: 6

Re: Padding of character variable with zeroes.

Posted in reply to snoopy369

Hello Snoopy...thanks for your reply and sorry for the confusion.

I attached a file below where the two dataset's are produced by means procedure and the zip data was just an example which was inappropriate I guess..the  dataset which is below, has all of the correct values in all the variables however in the above dataset, please have a look at chg_minc character variable  that has value of -.278. I want to pad this value with zero after negative sign so it can be displayed as -0.278 so I used this line of code want=put(input(min,best.),z6.3) and I am getting the right answer

but however the values where there is no negative sign I want the format to be 5.3 and not 6.3 so what's the best way to do this?

 

 

Highlighted
Super User
Posts: 20,716

Re: Padding of character variable with zeroes.

Posted in reply to Aidaan_10

I feel like this isn't een the original question. 

 

You can nest formats.

 

proc format;
value myfmt
low - 0 = [z5.3]
0-high = [6.2];
run;

Super Contributor
Posts: 311

Re: Padding of character variable with zeroes.

[ Edited ]
Posted in reply to Aidaan_10

I think it's unclear to me at least exactly what your rules are.  You mention 'digits' and have (and want, apparently?) decimals, but you don't discuss how they (and negatives) count towards digits.

 

Can you put the "desired" for all of these?

 

The initial problem is that INPUT works off character, so you have to first PUT your number to a character before anything else happens.

 

One possibility is the following; it would be helpful to explain where this is not correct.

 

data have;
input zip;
datalines;
123456789
-123456789
1234
-.234
12345
-.23456789
12345
12345
1234
123456789
;;;;
run;


data want;
  set have;
  zip_c = put(input(substr(put(zip,12.3 -l),1,6),6.),z6.3);
run;

Notice I first PUT the zip to a large-enough string; then I substring it to 6 digits; then I input it; then I put it back with zero padding.  This doesn't exactly match what you indicate though, but it depends on how the negative sign and decimal are supposed to work, really.

SAS Moderator
Posts: 17

Re: Padding of character variable with zeroes. [how to improve your question]

Posted in reply to Aidaan_10

Hello @Aidaan_10,


Your question requires more details before experts can help. Can you revise your question to include more information? 

 

Review this checklist:

  • Specify a meaningful subject line for your topic.  Avoid generic subjects like "need help," "SAS query," or "urgent."
  • When appropriate, provide sample data in text or DATA step format.  See this article for one method you can use.
  • If you're encountering an error in SAS, include the SAS log or a screenshot of the error condition. Use the Photos button to include the image in your message.
    use_buttons.png
  • It also helps to include an example (table or picture) of the result that you're trying to achieve.

To edit your original message, select the "blue gear" icon at the top of the message and select Edit Message.  From there you can adjust the title and add more details to the body of the message.  Or, simply reply to this message with any additional information you can supply.

 

edit_post.png

SAS experts are eager to help -- help them by providing as much detail as you can.

 

This prewritten response was triggered for you by fellow SAS Support Communities member @ballardw

.
Super User
Posts: 11,779

Re: Padding of character variable with zeroes.

Posted in reply to Aidaan_10

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Super Contributor
Posts: 311

Re: Padding of character variable with zeroes.

Ask a Question
Discussion stats
  • 9 replies
  • 166 views
  • 2 likes
  • 5 in conversation