I want to convert a character variable (stub) to a new variable (stub1). Rules are that the first 5 or 6 characters before m or e are kept (e.g, B01001e1 -> keep B01001 and B01001Ee -> keep B01001E) and that numbers after m or e are added to three digits (e.g. 1 -> 001, 12 -> 012, 123 -> 123).
For example,
B01001e1 -> B01001001
B02001m12 = B02001012
B01001Ee1 -> B01001E001
B05003Fm123 -> B05003F123
How can I deal with this matter? Thank you so much
I knew it. . Here is another variation:
data want;
set have;
substr(var,length(scan(var,1,'me'))+1)=put(input(scan(var,2,'me'),best.),z3.);
run;
Haikuo
You can look at some string functions such as compress using e/m as delimiters as well as translate to do the conversion for the other characters, or a format that does the conversion for the second part would also work.
This is to follow Fareeza's logic. I am sure there would be better code than the following:
data have;
input var $20.;
cards;
B01001e1
B02001m12
B01001Ee1
B05003Fm123
;
data want;
set have;
_l=length(scan(var,1,'me'));
newvar=cats(substr(var,1,_l+1),put(input(substr(var,_l+2),best.),z3.));
drop _l;
run;
Haikuo
same but different.
I knew it. . Here is another variation:
data want;
set have;
substr(var,length(scan(var,1,'me'))+1)=put(input(scan(var,2,'me'),best.),z3.);
run;
Haikuo
Hi data_null_, Reeza,
Thank you so much for your help,
Hi Hai.kuo,
Thank you so much for your nice sample code.
Best Regards,
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.