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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 16359 views
  • 2 likes
  • 4 in conversation