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

Solved
Occasional Contributor
Posts: 13

# 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

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

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

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

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

Thank you very much!

Occasional Contributor
Posts: 13