BookmarkSubscribeRSS Feed
kingCobra
Obsidian | Level 7

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.

7 REPLIES 7
Astounding
PROC Star

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.

KrisDeng
Obsidian | Level 7
The code you provided is so simple yet effective, I was able to solve the problem Im having immediately.

May I ask what is the role of the numbers "1" "2" and "3" is the middle? And the sign " | " too?

Thank you very much.
Astounding
PROC Star

The basics:  the SCAN function extracts words from a string.  The second parameter (1, 2, 3) indicates which word to extract.  You can find all the details about SCAN in the online documentation.

 

Two vertical bars join character strings.  There are many more modern functions that can accomplish all of that and more (CAT, CATT, CATS, CATX).

 

KachiM
Rhodochrosite | Level 12

Try

@'('

Then adjust variables.

data_null__
Jade | Level 19

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
slchen
Lapis Lazuli | Level 10

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;

kingCobra
Obsidian | Level 7

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

Thank you for helping.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 7 replies
  • 6447 views
  • 3 likes
  • 6 in conversation