DATA Step, Macro, Functions and more

extracting the part of string

Accepted Solution Solved
Reply
Regular Contributor
Posts: 234
Accepted Solution

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
Trusted Advisor
Posts: 1,115

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;

View solution in original post


All Replies
Respected Advisor
Posts: 3,777

Re: extracting the part of string

 

 

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

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.

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

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