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
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;
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
Add sufficient variables to the ARRAY statement to cover your worst case.
it works. Thanks ballardw!
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.