BookmarkSubscribeRSS Feed
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

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

6 REPLIES 6
japelin
Rhodochrosite | Level 12
Can you provide specific samples of the original data and desired results?
ballardw
Super User

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.

GS2
Obsidian | Level 7 GS2
Obsidian | Level 7
The variable is a character and needs to remain that way due to required formats for these data. Mostly 4 digits as in anything less than 1000, I have replaced with ‘xxx-xx-xxxx’ but some remain that are 6 digits or the full 9 digits. I will handle those on an individual basis as there are not many. If I can take ‘1234’ as a character variable and create ‘xxx-xx-1234’ still as a character variable, that would be ideal. Thank you
ballardw
Super User

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.

GS2
Obsidian | Level 7 GS2
Obsidian | Level 7
I checked my code and I previously set the length to 20 to make sure I had room for any longer strings that could be in the data set. Is there any options for this scenario?

Also, fakessn. is not a format I have available. Thank you
SASKiwi
PROC Star

@ballardw posted the code for the FAKESSN format earlier in your post.

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
  • 6 replies
  • 1709 views
  • 1 like
  • 4 in conversation