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

I have seen this issue addressed in other posts and I know that a statement like:

put(v1,z5.);

requires that v1 be numeric.

I have multiple datasets that I am trying to append where sometime v1 is numeric and sometimes is character--v1 is a zipcode and when it is numeric the leading zero sometimes needs to be restored. In files where the zipcode is charachter, the lengths vary from $5 to $10.

So I want to standardize all zipcodes before appending.

I have tried 2 approaches using the TYPE column from a PROC CONTENTS output to determine how to treat the field in different files, so I can build 1 macro to handle both types of cases.

Approach 1:

data t;

input @1 zip $3. @5 typ 8.;

cards;

949 2

31  2

768 2

74  2

270 2

173 2

100 2

755 2

476 2

454 2

415 2

;

data t_;

set t;

if typ=2 then zipcode=put(input(zip,8.),z5.);

else if typ=1 then zipcode=put(z,z5.);

run;

Approach 2:

data t;

input @1 zip $3. @5 typ 8.;

cards;

949 2

31  2

768 2

74  2

270 2

173 2

100 2

755 2

476 2

454 2

415 2

;

data t_;

set t;

if typ=2 then goto zchar;

else goto znum;

zchar:

zipcode=put(input(zip,8.),z5.);

goto continue;

znum:

zipcode=put(zip,z5.);

goto continue;

continue:

x1=typ*9;

x2=typ/x1;

x3=(.985*typ)+(.375*x2);

run;

Both approaches fail to pass over the if-then-else condition that does not apply.

Any suggestions? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

508   data _null_;

509     char='12345';

510     num=12345;

511     zip1 = put(input(cats(char),8.),z5.);

512     zip2 = put(input(cats(num),8.),z5.);

513     put (_all_) (=);

514   run;

char=12345 num=12345 zip1=12345 zip2=12345

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

508   data _null_;

509     char='12345';

510     num=12345;

511     zip1 = put(input(cats(char),8.),z5.);

512     zip2 = put(input(cats(num),8.),z5.);

513     put (_all_) (=);

514   run;

char=12345 num=12345 zip1=12345 zip2=12345

BrucePrater
Calcite | Level 5

Excellent sir!

Thank you so much.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 2 replies
  • 20288 views
  • 0 likes
  • 2 in conversation