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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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,
Jag

View solution in original post

19 REPLIES 19
andreas_lds
Jade | Level 19

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;
vraj1
Quartz | Level 8

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?

 

Jagadishkatam
Amethyst | Level 16

Please try

 

data want;
set have;
len=length(tm);
ex=input(tranwrd(substr(tm,len-3),'_','.'),best.)*34;
run;
Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

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;

 

Thanks,
Jag
vraj1
Quartz | Level 8

it is not working.

Variable x is uninitialized.
NOTE: Invalid second argument to function SUBSTR at line 1

Jagadishkatam
Amethyst | Level 16

Sorry

 

try this

 

data want; 
set have; 
len=length(tm); 
ex=input(tranwrd(prxchange('s/^\_//',1,substr(tm,len-3)),'_','.'),best.)*34; 
run;
Thanks,
Jag
vraj1
Quartz | Level 8

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

Jagadishkatam
Amethyst | Level 16
Could you please explain what you mean by the earliest value like if you see the earliest values from 10*34, 12*34 and 3.5*34 is 119 which is from 3.5*34. There is a confusion here to understand the earliest value
Thanks,
Jag
vraj1
Quartz | Level 8

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

Jagadishkatam
Amethyst | Level 16
yes thank you i tried the same, hope it will help you
Thanks,
Jag
vraj1
Quartz | Level 8

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

Jagadishkatam
Amethyst | Level 16

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,
Jag
vraj1
Quartz | Level 8

Thanks a lot

slchen
Lapis Lazuli | Level 10
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.

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 19 replies
  • 3309 views
  • 3 likes
  • 6 in conversation