BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jenim514
Pyrite | Level 9

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20
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;
ballardw
Super User

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.

novinosrin
Tourmaline | Level 20

Sir @ballardw  Very clever indeed. But honestly in a prod environment and that too for a low/intermediate users can be a hard learning curve. Well even for experienced, the maintenance can be daunting unless the genius @ballardw  lives forever to support them.  Kudos!

novinosrin
Tourmaline | Level 20

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

Yavuz
Quartz | Level 8
I am a sas lover and trying to improove myself.
I really thanks to all of you. I learn new things from every replys, different ways...thanks again.

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 18187 views
  • 2 likes
  • 4 in conversation