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.
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;
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;
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
Thanks.
Nearly 200 sessions are now available on demand in the Innovate Hub.
Watch Now →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.