SAS 9.4
I have a data set with mostly 4 digit numbers and I would like to have them as a character variable "xxx-xx-4 digit number sequence'. Is there an efficient way to program this? Thank you
When you say "mostly 4 digit numbers" two question come to mind: Is the value actual a SAS numeric variable or character containing digits? and what kind of values are the not exactly 4 digits?
If the variable is numeric and none of the numeric values are greater than 9999 a custom format would likely be easiest.
proc format; picture fakessn (default=11) low-high = '9999' (prefix='xxx-xx-'); run;
In a picture the string '9999' means that 4 digits will be displayed so if a value is less than 1000 it will be padded with leading 0. The prefix text should be fairly obvious.
Associate the format with the variable when you want to display it. You would need to make sure the format was available in any SAS session that you want to use this.
You can run this code after the Proc Format is run to see what the results look like:
data _null_; do x = 0,1,10,100,1000,4567; put x fakessn.; end; run;
Look in the log to see the result.
My approach,
data want; set have; if length (ssnchar)=4 then newssnchar= put(input(ssnchar,4.),fakessn.)); run;
IF your existing character version is length 11 or greater you could use your existing SSN variable name in place of what I called Newssnchar. I made up names because you did not provide any variable name. It is a good idea whenever discussing character variables to tell us the defined length of the variable as well as names. Sometimes you may be able to place the desired value into an existing variable and otherwise not depending on the length.
@ballardw posted the code for the FAKESSN format earlier in your post.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.