DATA Step, Macro, Functions and more

Extract specific word from a string/variable

Reply
Occasional Contributor
Posts: 15

Extract specific word from a string/variable

Hello,

My variable looks like

Calcium (mmol/L) High

Creatinine Clearance (mL/min)

Albumin (g/L)

C-Reactive Protein (mg/L)

I want to create 2 variables as

Var1                                         Var 2

Calcium High                          mmol/L

Creatinine Clearance               mL/min

Albumin                                  g/L

C-Reactive Protein                  mg/L

Using SCAN function and then concatenating back looking cumbersome. Is there any function/ method which can extract the content within () without going through lot of manipulations?

Thanks in advance.

Super User
Posts: 5,085

Re: Extract specific word from a string/variable

Using SCAN shouldn't be too cumbersome.  You could try, for example:

var1 = scan(var, 1, '()') || scan(var, 3, '()');

var2 = scan(var, 2, '()');

The difficulty would lie in the possibility that there might be nonconforming structures to the data.  There might be two sets of values in parentheses, or a unit of measurement that is not in parentheses.  So this is a quick and dirty method, where parsing functions might be safer but more complex.

Good luck.

Super Contributor
Posts: 255

Re: Extract specific word from a string/variable

Try

@'('

Then adjust variables.

Respected Advisor
Posts: 3,777

Re: Extract specific word from a string/variable

This looks about right.

data param;
   input desc $50.;
  
retain rx;
   if _n_ eq 1 then rx = prxparse('s/\((.*)\)//');
   m = prxmatch(rx,desc);
  
if m then do;
      unit = prxposn(rx,
1,desc);
      call prxchange(rx,1,desc);
      desc = compbl(desc);
     
end;

  
cards;
Calcium (mmol/L) High
Creatinine Clearance (mL/min)
Albumin (g/L)
C-Reactive Protein (mg/L)
;;;;
   run;
proc print;
  
run;

Capture.PNG
Super Contributor
Posts: 275

Re: Extract specific word from a string/variable

data have;

   input string $50.;

   var1=prxchange('s/\(.*\)//',-1,string);

   var2=prxchange('s/.*\((.*)\).*/$1/',-1,string);

   cards;

Calcium (mmol/L) High

Creatinine Clearance (mL/min)

Albumin (g/L)

C-Reactive Protein (mg/L)

;

   run;

Occasional Contributor
Posts: 15

Re: Extract specific word from a string/variable

Thanks to everyone. I tried SLCHEN and DATA_NULL_; method and both worked.

Thank you for helping.

Ask a Question
Discussion stats
  • 5 replies
  • 522 views
  • 2 likes
  • 5 in conversation