I'm using zip codes, some of which start with a zero. I used the CATX, CATT, CATS, and CAT functions to try to concatenate a 0 then the remaining four digits to fix my current variable where the leading zero is missing. None of these worked. I'm left with a new variable with no leading zero for these. It's a character variable. (I need to fix this problem first because another issue I'll fix after this is dealing with xxxxx-xxxx zip codes. I don't think using LENGTH will fix my 4-digit issue since I'll need to chop off the correct side of my 9-digit ones later.)
data new; set old;
if length_zip=4 and state_code='CT' then zipcode=catt('0',zip);
if length_zip=4 and state_code='ME' then zipcode=catt('0',zip);
if length_zip=4 and state_code='MA' then zipcode=catt('0',zip);
if length_zip=4 and state_code='MA' then zipcode=catt('0',zip);
if length_zip=4 and state_code='NH' then zipcode=catt('0',zip);
if length_zip=4 and state_code='NJ' then zipcode=catt('0',zip);
if length_zip=4 and state_code='RI' then zipcode=catt('0',zip);
if length_zip=4 and state_code='VT' then zipcode=catt('0',zip);
else zipcode=zip; run;
The variable ZIPCODE must already exist on your input dataset and be defined as numeric.
So the CATT() is working fine but then SAS has to convert the string back into a number so the leading zero is gone again.
If you want to keep ZIPCODE as numeric then all you need to do is change the format so that it will display leading zeros.
format zipcode z5.;
If you want to make it character then you have to remove (or rename) the old variable.
data new;
set old(drop=zipcode);
zipcode=put(zip,z5.);
run;
Instead of the CAT_ functions try this. For either character ZIP or numeric ZIP.
data new;
/* ZIP here is a number */
zipNum=4321;
zipNumCode=put(zipNum,z5.);
/* ZIP here is a character */
zipChar='5432';
zipCharCode=put(input(zipChar,5.),z5.);
run;
Is your ZIPCODE variable numeric? How about ZIP? It might be educational to run proc contents on your data set OLD and show the results.
If your variable ZIP is numeric your length_zip may not be "4" as you think.
data example; zip= 1234; length_zip= length(zip); run;
Yields length_zip of 12.
If the data were read in correctly then the leading zero(s) would still be there. The reason they would disappear is if the were read as numeric values.
In which case you may find that your data actually doesn't have any 5+4 as the - character would render them non-numeric when read.
Please show the code on how you read your data into SAS.
I'm going to guess that you used Proc Import in some form, which will guess the variable type and length. And depending on file type does so examining a very few records.
The variable ZIPCODE must already exist on your input dataset and be defined as numeric.
So the CATT() is working fine but then SAS has to convert the string back into a number so the leading zero is gone again.
If you want to keep ZIPCODE as numeric then all you need to do is change the format so that it will display leading zeros.
format zipcode z5.;
If you want to make it character then you have to remove (or rename) the old variable.
data new;
set old(drop=zipcode);
zipcode=put(zip,z5.);
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.