- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Excellent sir!
Thank you so much.