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  

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 994 views
  • 1 like
  • 3 in conversation