## extracting the part of string

Solved
Super Contributor
Posts: 285

# extracting the part of string

How to get the last 4 digits of SSN from this data?

``````data SSN;
input SSN \$;
cards;
123456789
12345678
1234567
;
run;``````

Accepted Solutions
Solution
‎02-04-2016 04:51 PM
Posts: 1,256

## Re: extracting the part of string

With your data step you lose already the "9" in the first row.

``````data SSN;
input SSN :\$9.; /* The default length 8 is insufficient! */
cards;
123456789
12345678
1234567
;
run;

data want;
set ssn;
length last4d \$4;
last4d=substr(ssn,length(ssn)-3);
run;``````

All Replies
Posts: 3,852

## Re: extracting the part of string

``last4 = substrn(ssn,lengthn(ssn)-4+1);``
Solution
‎02-04-2016 04:51 PM
Posts: 1,256

## Re: extracting the part of string

With your data step you lose already the "9" in the first row.

``````data SSN;
input SSN :\$9.; /* The default length 8 is insufficient! */
cards;
123456789
12345678
1234567
;
run;

data want;
set ssn;
length last4d \$4;
last4d=substr(ssn,length(ssn)-3);
run;``````
🔒 This topic is solved and locked.

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