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

I have a dataset that has census tract fips codes, but the leading 0 is getting cut off. I was able to put a leading 0 in front of the county fips code using substr and length, but the same thing is not working for census tract. I keep getting errors.

 

Basically, I have ct fips that look like this:

1001020100

1001020200

24006840030

24007850040

 

And I want it to look like this (notice that the last two remain the same as they already have 11 digits);

01001020100

01001020200

24006840030

24007850040

 

I want to leave these as character variables. In the code, I put

 

data want;

    set have;

    ct = substr(ctfips, length(ctfips)-10, 11);

    drop ctfips;

    rename ct = ctfips;

run;

 

Not sure if that's what I need to be doing, but it's not giving the leading zero for a total length of 11. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Krueger
Pyrite | Level 9

Modified previous solution to fit what you are doing. https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/td-p...

 

data test;
    length charvar $ 11 padded $ 11;
    infile datalines;
    input charvar;
    /* read charvar as number, then format with leading zeroes */
    /* then put result into padded */
    padded = put(input(charvar,best11.),z11.);
datalines;
1001020100
1001020200
24006840030
24007850040
;
run;

View solution in original post

6 REPLIES 6
Krueger
Pyrite | Level 9

Modified previous solution to fit what you are doing. https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/td-p...

 

data test;
    length charvar $ 11 padded $ 11;
    infile datalines;
    input charvar;
    /* read charvar as number, then format with leading zeroes */
    /* then put result into padded */
    padded = put(input(charvar,best11.),z11.);
datalines;
1001020100
1001020200
24006840030
24007850040
;
run;
Reeza
Super User
Where exactly in that code do you think you're adding a leading zero?
novinosrin
Tourmaline | Level 20

Hi @wernie  The great Xia Keshan @Ksharp  answered this a while ago

 


data have;
input str $11.;
cards;
1001020100
1001020200
24006840030
24007850040
;

data want;
 set have;
 length want $11;
 want=translate(right(str),'0',' ');
run;
Reeza
Super User
Just a quick note that if you want to save the results to the same variable the length statement needs to be before the SET statement. If you're creating a new variable this is not an issue.
novinosrin
Tourmaline | Level 20

Oh yes indeed. Honestly, I didn't pay attention to whether the OP wants the same input variable value to be modified rather than a new assignment. Very good and important point

Tom
Super User Tom
Super User

If the values can actually be converted to integers then you can use the Z format to convert it back with leading zeros.  So use INPUT() to convert to a number and PUT() to convert back to string.

ctfips = put(input(ctfips,11.),Z11.);

Now if the values either contain non-digit characters, or there are more than 15 characters then you will want to use character operations instead. Such as moving the spaces to the front and replacing them with zeros.

ctfips = translate(right(substr(ctfips,1,11)),'0',' ');

Or prefixing zeros.

if length(ctfips) < 11 then ctfips = repeat('0',11-length(ctfips)-1)||ctfips;

Or

ctfips = reverse(substr(reverse(cats(repeat('0',11-1),ctfips)),1,11));

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 6 replies
  • 796 views
  • 4 likes
  • 5 in conversation