BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DanielQuay
Quartz | Level 8

 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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

12 REPLIES 12
Reeza
Super User
Usual recommendation is to add leading zeros, not trailing zeros. Hard to know which is useful that way.

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.
DanielQuay
Quartz | Level 8

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;

Reeza
Super User
ID2_trailing = catt(ID, '000');

ID2_leading = put(ID, z13.);

ballardw
Super User

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.

 

 

Tom
Super User Tom
Super User

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.

DanielQuay
Quartz | Level 8

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.

Kurt_Bremser
Super User

"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.

DanielQuay
Quartz | Level 8
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.
Kurt_Bremser
Super User

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;
DanielQuay
Quartz | Level 8

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.

Kurt_Bremser
Super User

@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.

Tom
Super User Tom
Super User

@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?

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 8950 views
  • 4 likes
  • 5 in conversation