SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 21478 views
  • 0 likes
  • 2 in conversation