data have; length ssn $9; input ssn; datalines; 000000000 112334588 426-277 ;run;
Creates the following output
ssn
000000000
112334588
426-277
I need to do the following;
if the ssn is a blank or if there is any character except consecutive 9 numbers I need it to show as 000000000 where this is a character
Does compress work in this situation? I have not really used compress much. So in the example here I need for 426-277 to read 000426277
Instead of using compress to remove characters, you can use it to keep characters, as in:
ssn=compress(ssn,'0123456789','k');
where 'k' says to keep the characters in the 2nd argument.
You can then prefix the resulting ssn with 9 zeroes, and take a substr of the last 9 characters:
data have;
length ssn $9;
input ssn $9.;
datalines;
000000000
112334588
426-277
run;
data want;
set have;
put 'Before: ' ssn= @;
ssn=compress(ssn,'0123456789','k');
put @25 'between: ' ssn= @ ;
ssn=substr(cats('000000000',ssn),lengthn(ssn)+1);
put @45 ' After:' ssn ;
run;
The substr function tells sas to take characters starting at position lengthn(ssn)+1. lengthn will return a 0 for a blank argument, while length will return a 1). I've added a fourth blank SSN to the sample data.
This means for ssn='426277' (after the first compress), you would get
which means the substr starts in position 7 of 000000000426277, yielding 000426277.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.