DATA Step, Macro, Functions and more

I am trying to separate values of a variable into 2 different variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

I am trying to separate values of a variable into 2 different variables

 

I have a variable with values as follows:

med_qty

100ml

2*96

2*100g

 

I want to make the above data into :

 

 dose    dosage      qty  

 100                        ml

   96         2    

 100          2            g


Accepted Solutions
Solution
‎04-10-2018 11:18 PM
PROC Star
Posts: 8,146

Re: I am trying to separate values of a variable into 2 different variables

Posted in reply to sas_learnsups

Here is one way:

 

data want (drop=med_qty);
  set have;
  if anyalpha(med_qty) then do;
   qty=substr(med_qty,anyalpha(med_qty));
   med_qty=substr(med_qty,1,anyalpha(med_qty)-1);
  end;
  dose=input(scan(med_qty,1,'*'),8.);
  dosage=input(scan(med_qty,2,'*'),8.);
run;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
Solution
‎04-10-2018 11:18 PM
PROC Star
Posts: 8,146

Re: I am trying to separate values of a variable into 2 different variables

Posted in reply to sas_learnsups

Here is one way:

 

data want (drop=med_qty);
  set have;
  if anyalpha(med_qty) then do;
   qty=substr(med_qty,anyalpha(med_qty));
   med_qty=substr(med_qty,1,anyalpha(med_qty)-1);
  end;
  dose=input(scan(med_qty,1,'*'),8.);
  dosage=input(scan(med_qty,2,'*'),8.);
run;

Art, CEO, AnalystFinder.com

 

PROC Star
Posts: 1,570

Re: I am trying to separate values of a variable into 2 different variables

Posted in reply to sas_learnsups
data have;
input  med_qty $20.;
datalines;
100ml
2*96
2*100g
;

data want;
set have;
if index( med_qty, '*')>0 then do;
dose=compress(scan(med_qty,2,'*'),,'kd');
dosage=scan(med_qty,1,'*');
qty=compress(med_qty,'*','d');
end;
else do;
dose=compress(med_qty,,'kd');
qty=compress(med_qty,'*','d');
end;
run;
Occasional Contributor
Posts: 13

Re: I am trying to separate values of a variable into 2 different variables

Posted in reply to novinosrin

Thank you very much!

Occasional Contributor
Posts: 13

Re: I am trying to separate values of a variable into 2 different variables

Posted in reply to novinosrin

Thank you very much

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 100 views
  • 2 likes
  • 3 in conversation