i have a variable called tm. It has character values like "DGM_HIGH_IG_DUR24_10_0" and "CGM_HIGH_IG_DUR24_12_0" and "CGM_LOW_IG_DUR24_3_5".
I want to extract _10_0 from first and _12_0 and _3_5 from second and third and calculate 10.0*34 and 12.0*34 and 3.5*34 and replace the value with the ealier ones.
Can anyone help me in this please
ok i understood, please try
data have;
length ex2 $50;
tm="CGM_HIGH_IG_DUR24_182_8";
len=length(tm);
dec=scan(put(input(tranwrd(substr(tm,prxmatch('m/\_\d/',tm)+1),'_','.'),best.)*34,best.),2,'.');
if dec=. then ex='_'||strip(put(input(tranwrd(substr(tm,prxmatch('m/\_\d/',tm)+1),'_','.'),best.)*34,best.))||'_0';
else if dec^=. then ex='_'||tranwrd(strip(put(input(tranwrd(substr(tm,prxmatch('m/\_\d/',tm)+1),'_','.'),best.)*34,best.)),'.','_');
ex2=cats(prxchange('s/\_\d*\_\d//',1,tm),ex);
run;
Using the functions scan and input:
data work.have; length tm $ 30; input tm; datalines; DGM_HIGH_IG_DUR24_10_0 CGM_HIGH_IG_DUR24_12_0 CGM_LOW_IG_DUR24_3_5 run; data work.want; set work.have; length result 8; result = 34 * (input(scan(tm, 2, '_', 'b'), best32.) + input(scan(tm, 1, '_', 'b'), best32.) / 10); run;
Thanks a lot 🙂
Can i replace the new values i.e result values with the old ones for example if i get 180.2 i need to replace it
DGM_HIGH_IG_DUR24_180_2
How can i do that please?
Please try
data want;
set have;
len=length(tm);
ex=input(tranwrd(substr(tm,len-3),'_','.'),best.)*34;
run;
Please try the below which will work
data want; set have; len=length(tm); ex=input(tranwrd(prxchange('s/^\_//',1,substr(x,len-3)),'_','.'),best.)*34; run;
it is not working.
Variable x is uninitialized.
NOTE: Invalid second argument to function SUBSTR at line 1
Sorry
try this
data want;
set have;
len=length(tm);
ex=input(tranwrd(prxchange('s/^\_//',1,substr(tm,len-3)),'_','.'),best.)*34;
run;
Thanks. This is coming for me but i want to replace the new values with the old one.
for ex: CGM_HIGH_IG_DUR24_10_0 and ex value for this is 180.2 then it needs to be CGM_HIGH_IG_DUR24_180_2
yes for example i have a value in tmp called CGM_HIGH_IG_DUR24_10_0 and after calculation i.e 10.0*18=180.0 then i need to replace CGM_HIGH_IG_DUR24_10_0 to CGM_HIGH_IG_DUR24_180_0.
Hope you got it
Here there is a small catch. if the value is 132.6 then it should come as 132_6 but we are getting CGM_LOW_IG_DUR24_132.6_0
ok i understood, please try
data have;
length ex2 $50;
tm="CGM_HIGH_IG_DUR24_182_8";
len=length(tm);
dec=scan(put(input(tranwrd(substr(tm,prxmatch('m/\_\d/',tm)+1),'_','.'),best.)*34,best.),2,'.');
if dec=. then ex='_'||strip(put(input(tranwrd(substr(tm,prxmatch('m/\_\d/',tm)+1),'_','.'),best.)*34,best.))||'_0';
else if dec^=. then ex='_'||tranwrd(strip(put(input(tranwrd(substr(tm,prxmatch('m/\_\d/',tm)+1),'_','.'),best.)*34,best.)),'.','_');
ex2=cats(prxchange('s/\_\d*\_\d//',1,tm),ex);
run;
Thanks a lot
data have;
length tm $ 30;
input tm;
val=prxchange('s/.*_(\d+?.?\d?)_(\d)$/$1.$2/',-1,strip(tm));
if count(val,'.')>1 then val=prxchange('s/(.*).(\d)$/$1$2/',-1,strip(val));
val=34*(input(val,best12.));
datalines;
DGM_HIGH_IG_DUR24_10_0
CGM_HIGH_IG_DUR24_12_0
CGM_LOW_IG_DUR24_3_5
DGM_HIGH_IG_DUR24_180_2
CGM_LOW_IG_DUR24_132.6_0
;
run;
proc print;
run;
with CGM_LOW_IG_DUR24_132.6_0 in data set.
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.