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:
padded = put(input(charvar,best.),zw.d);
Thank you
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
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
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?
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;
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.
Hello @Aidaan_10,
Your question requires more details before experts can help. Can you revise your question to include more information?
Review this checklist:
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.
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
.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.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: