BookmarkSubscribeRSS Feed
michellel
Calcite | Level 5

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

5 REPLIES 5
slchen
Lapis Lazuli | Level 10

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;

michellel
Calcite | Level 5

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



ballardw
Super User

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

michellel
Calcite | Level 5

it works. Thanks ballardw!

Ksharp
Super User

Code: Program

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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 2171 views
  • 0 likes
  • 4 in conversation