Hi,
I have a variable with values all structured like this:
MHS7. VITAMIN D DEFICIENCY (ONSET DATE = UN UNK 2016)
I need to capture the 'VITAMIN D DEFICIENCY' only. So the text string between the ' . ' and the ' ( '
Is there a way to do this with a scan or some other way?
Thanks!
Another way:
data want; str='MHS7. VITAMIN D DEFICIENCY (ONSET DATE = UN UNK 2016)'; length newstr $ 25; newstr = strip(scan(str,2,'.(')); run;
The SCAN function can be provided explicit delimiters, in this case the period and left parentheses. Since the value you want would have a leading blank I add strip to remove that.
Without knowing other possible values needed it is a good idea to set a length for the new variable to hold the longest expected length. Otherwise your length would be set from the first record and might not be as long as needed for other records.
data want;
str='MHS7. VITAMIN D DEFICIENCY (ONSET DATE = UN UNK 2016)';
pos1=findc(str, '.');
pos2=findc(str, '(');
Need=substr(str,Pos1+1,pos2-1-pos1);
drop pos:;
run;
Another way:
data want; str='MHS7. VITAMIN D DEFICIENCY (ONSET DATE = UN UNK 2016)'; length newstr $ 25; newstr = strip(scan(str,2,'.(')); run;
The SCAN function can be provided explicit delimiters, in this case the period and left parentheses. Since the value you want would have a leading blank I add strip to remove that.
Without knowing other possible values needed it is a good idea to set a length for the new variable to hold the longest expected length. Otherwise your length would be set from the first record and might not be as long as needed for other records.
Also if you may and have time, for the sake of others, a note on how scan treats various combination of delimiters with/without modifiers will help. My $0.02 cents
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.