I have a number of ID variables. They are ten characters long but I need them to be thirteen characters long to match the data dictionary.
The suggestion was that I simply add three trailing zeros to each case but I can't seem to figure out how to do that in SAS.
So you need to multiply those values that are below a certain threshold:
data have;
input code;
format code 13.;
datalines;
2934567890
2912345678000
;
data want;
set have;
if code lt 10000000000
then code = code * 1000;
run;
So the data is numeric, but the z format option doesn't appear to be doing it. And for this specific request it needs to be trailing zero's. The system it's going into can't handle leading zeros.
So I'm trying:
data want;
set have;
ID2 = catt(ID, z15.);
run;
It sounds like you may need to confirm that the "other data" is actually numeric.
If your values are supposed to be numeric they should be in the correct range numerically. The fact that you need trailing zeroes means that you values are 10, 100 or 1000 times too small. ie a value if a value of 1 needs three zeroes then the numeric value needs to be 1000. Which would lead me back to how did you read the original value such that it missed 3 significant (or possibly more?) digits when read. This looks like a time to investigate the cause, likely at the read step, and address it there. Then no "fixing" would needed.
So you have numeric values and you want to add three extra zeros? Just multiply by 1,000.
But the request does not really make much sense, something seems missing in your explanation of the problem.
Numbers do not have a "number of characters" unless you are writing them into text. If you want to print the values into a text file and have them always use 10 character positions then just print them with the 10. (or Z10. ) format.
Sorry, sick kid been away from the computer for a bit.
Anyway okay I can break down the why of this request.
I was handed a raw dataset with a unique ID variable that was structured to the original request.
This has changed and they now need the unique ID variable to be longer (numeric data).
The data structure cannot have leading zero's because it requires the first two to be a state FIPS code identifier.
As such I need to add extra characters to the end of the unique ID from the raw dataset, but they still want it to be a numeric data type.
I get that it's not the usual way to do things, but it's what I got handed.
"The data structure cannot have leading zero's because it requires the first two to be a state FIPS code identifier."
According to https://en.wikipedia.org/wiki/Federal_Information_Processing_Standard_state_code, several state codes (e.g. Alabama - 01) start with a zero.
So you need to multiply those values that are below a certain threshold:
data have;
input code;
format code 13.;
datalines;
2934567890
2912345678000
;
data want;
set have;
if code lt 10000000000
then code = code * 1000;
run;
THis got it, but I had to add a format for it to print properly to the CSV txt file I had to send them.
@DanielQuay wrote:
THis got it, but I had to add a format for it to print properly to the CSV txt file I had to send them.
Now wait, all this for a TEXT file???
Then there is absolutely no need to handle this as numbers in SAS.
@DanielQuay wrote:
Yes, and it reads those as being assigned to that state. The first two numbers have to be 29 for my dataset. if it reads 00 then it will return an error because that is an invalid FIPS code.
Perhaps if you can explain more about what you are doing then an easier, clearer way forward can be found.
Are you using this data with other SAS code? Do you have access to the source code?
Are you passing the data from your SAS dataset to some other system to use? How are you transferring the data? Are you writing a text file for the other system to read? What is the format required for that text file?
Are you pushing data into an external database? How are the fields defined in that external database?
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.