BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10
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

1 REPLY 1
mkeintz
PROC Star

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

  • 000000000426277  as the first arg of the 2nd compress
  • 7  as the 2nd arg of the 2nd compress

which means the substr starts in position 7 of 000000000426277, yielding   000426277.

    

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1 reply
  • 849 views
  • 1 like
  • 2 in conversation