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.
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.
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).
Try
@'('
Then adjust variables.
This looks about right.
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;
Thanks to everyone. I tried SLCHEN and DATA_NULL_; method and both worked.
Thank you for helping.
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 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.