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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

 

View solution in original post

3 REPLIES 3
JerryV
SAS Employee

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;

 

ballardw
Super User

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.

Tom
Super User Tom
Super User

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-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
  • 3 replies
  • 2569 views
  • 1 like
  • 4 in conversation