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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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