BookmarkSubscribeRSS Feed
Whitlea
Obsidian | Level 7

I am trying to create categories from open ended questions.  What is the best way to go about this if I want to use full strings?  Is there a way to do this without identifying position or case e.g (lowercase)?  I have attached code so you guys can get an idea of what I am trying to do, I am sure the index function probably is not the best way to go about this.  I basically want to tell say, " if a response in var contains this word and/or string, then var=1 ..."

SAS base 9.4

3 REPLIES 3
Astounding
PROC Star

Two things to consider ...

 

You are probably better off using INDEXW rather than INDEX.  If you are searching for "dense" SAS looks for those characters.  If your longer string were to contain "condense" then INDEX will find "dense", but INDEXW will not.

 

And it's easy to apply UPCASE to both arguments with INDEXW so matches can be found regardless of upper vs. lower case.

Shmuel
Garnet | Level 18

Assuming you are testing for whole words and there may be more than one wanted, you can try alternative code:

data want;
 set have;
      length word $15; /* addapt to max length word to check */
      i = 1;
      word = lowcase(scan(id01q01txt, i));
      do until (word = ' ');
           if word in ('cost' 'insurance' 'afford'  ...) then reasonmam=1; else
           if word in ('busy' 'availability' 'time' ... ) then reasonmam=2; else
           ... etc. up to ...
           if word in ('age' 'old' 'over' ...) reasonmam=10; 
   
           i+1;       
           word = lowcase(scan(id01q01txt, i));
          
          OUTPUT;   /* any time a word is fitting */
      end;
run;
Whitlea
Obsidian | Level 7

I will give this a try. Thank You!

 

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
  • 3 replies
  • 861 views
  • 0 likes
  • 3 in conversation