BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
u39734216
Fluorite | Level 6

Hi all,

 

I am working with data sets which have names like 

Nichols, KregFI
Reddy, RamanaBT

And I have to find the first name as Kreg and Ramana. pls, can anyone help me how to find it?
I was trying with this code SUBSTR(name,LENGTH(name,",")-1, -2); . but it was giving me an error.

1 ACCEPTED SOLUTION

Accepted Solutions
TarunKumar
Pyrite | Level 9

data have;
input custname:$15.;
datalines;
Nichols,KregFI
Reddy,RamanaBT
;run;

 

data want;
set have;
first_name = substr(scan(custname,-1,','),1,length(scan(custname,-1,','))-2);
run;

View solution in original post

3 REPLIES 3
TarunKumar
Pyrite | Level 9

data have;
input custname:$15.;
datalines;
Nichols,KregFI
Reddy,RamanaBT
;run;

 

data want;
set have;
first_name = substr(scan(custname,-1,','),1,length(scan(custname,-1,','))-2);
run;

Kurt_Bremser
Super User

If it's always two characters that need to be cut away, do this:

data have;
input name $30.;
datalines;
Nichols, KregFI
Reddy, RamanaBT
;

data want;
set have;
firstname = scan(substr(name,1,length(name)-2),2,',');
run;

proc print data=want noobs;
run;

Result:

     name          firstname

Nichols, KregFI     Kreg    
Reddy, RamanaBT     Ramana  

hackathon24-white-horiz.png

Join the 2025 SAS Hackathon!

Calling all data scientists and open-source enthusiasts! Want to solve real problems that impact your company or the world? Register to hack by August 31st!

Register Now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1142 views
  • 1 like
  • 3 in conversation