DATA Step, Macro, Functions and more

Adding characters to a string

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

Adding characters to a string

I am supposed to create a variable called SSN2. I currently have a variable called SSN that has the social security numbers in a format XXXXXXXXX, and want SSN2 to be in the format XXX-XX-XXXX, in other words adding hyphens in specific positions. I have tried using the CATX function, but I don't know if this is even the right function as I am just splitting the one variable up. Should I instead be using the SUBSTR function?


Accepted Solutions
Solution
2 weeks ago
Super User
Posts: 19,815

Re: Adding characters to a string

Posted in reply to marianhabesland

Or neither, use a format. SAS even has a format...

https://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm#n1sroiq0fql...

 

Convert your SSN to numeric using INPUT and then apply the format. 

 

 

View solution in original post


All Replies
Solution
2 weeks ago
Super User
Posts: 19,815

Re: Adding characters to a string

Posted in reply to marianhabesland

Or neither, use a format. SAS even has a format...

https://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm#n1sroiq0fql...

 

Convert your SSN to numeric using INPUT and then apply the format. 

 

 

Frequent Contributor
Posts: 77

Re: Adding characters to a string

Posted in reply to marianhabesland

 

 

data have;
input ss_no $9.;
datalines;
absdefght
abadefght
absdefgtk
absdefgtp
absdefert
asedrftgh
potrpshrt
;run;

data want;
set have;
ss_no_new = catx("-",substr(ss_no,1,3),substr(ss_no,4,2),substr(ss_no,6,4));
run;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 140 views
  • 2 likes
  • 3 in conversation