- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
There are many ways to do this but if all are 10 this is easy. Assuming this is character vairable:
if length(variable) = 10 then ID_new = catt('000', variable);
If you really want it trailing, reverse the order of the arguments in the function.
If your variable is numeric you can use the Z13 format to accomplish this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
ID2_leading = put(ID, z13.);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
"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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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?