Contributor
Posts: 71

# how to devide the context of 1 variable into several variables

Hi, I have one variable, which context looks as below. I need separate the original variable into the most 4 new variables, so I can do calculation and also can check unit. The digits of number are not the same, so it is hard to use substr function. Really appreciate your help!

var_original               var_want1  var_want2  var_want3  var_want4

500 MG-7.5 MG          500            MG          7.5          MG

50 MG/hl                    50             MG/hl

20 MG-5 MG               20              MG          5            MG

Super Contributor
Posts: 275

## Re: how to devide the context of 1 variable into several variables

data have;

input var_original \$20.;

array vars (4)\$ var1-var4;

do i=1 by 1;

if missing(scan(var_original,i,' -')) then return;

vars(i)=scan(var_original,i,' -');

end;

drop i;

cards;

500 MG-7.5 MG

50 MG/hl

20 MG-5 MG

;

run;

Contributor
Posts: 71

## Re: how to devide the context of 1 variable into several variables

Thanks slchen| Your code works, but actually I missed providing another type of example as below.

10 MG/5 ML-6.25 MG/5 ML

In this case, there will be ERROR like the following.

ERROR: Array subscript out of range at line 21 column 3.

STRNGTH=10 MG/5 ML-6.25 MG/5 ML

var1=10 var2=MG/5 var3=ML

var4=6.25 _ERROR_=1 _N_=168

Super User
Posts: 13,502

## Re: how to devide the context of 1 variable into several variables

Add sufficient variables to the ARRAY statement to cover your worst case.

Contributor
Posts: 71

## Re: how to devide the context of 1 variable into several variables

it works. Thanks ballardw!

Super User
Posts: 10,766

## Re: how to devide the context of 1 variable into several variables

`data have;  input text \$20.;  cards;500 MG-7.5 MG  50 MG/hl  20 MG-5 MG;run;data temp; set have;ExpressionID = prxparse('([\d.]+|[^\d.]+)');start = 1;stop = length(text);call prxnext(ExpressionID, start, stop, text, position, length);do while (position > 0);found = substr(text, position, length);output;call prxnext(ExpressionID, start, stop, text, position, length);end;keep text found;run;proc transpose data=temp out=want(drop=_:) prefix=var_want;by text notsorted;var found;run;`