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

Sample string-

Hobbies= Reading and singing.

How to store Reading and singing in two separate variables such as Hobby 1 and Hobby 2? 

Hobby1= Reading

Hobby2= Singing

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

You gave only one example and did not relate to the example I have posted.

 

For your specific example you can use next code, though it's not a complete, because

you will get a bad result for the 2nd example. Check output.

data test;
   infile cards truncover;
   input hobbies $char50. ;
cards;
Reading and Singing
I like to read books and to draw pictures
;
run;
data want;
 set test;
    do i=1 to countw(hobbies);
       if scan(lowcase(hobbies),i) in ('and', 'or') then do;
          hobby1 = scan(hobbies, i-1);
          hobby2 = scan(hobbies, i+1);
          output;
       end;
    end;
run;

To get a better code you need to post more examples with the desired output.

 

Assuming hobbies are usually verbs, it may be more difficult to decide by a program which word is a verb.

View solution in original post

7 REPLIES 7
Shmuel
Garnet | Level 18

Are you looking for verbs?

What would you subset into verbs having a text like:

  a = "I like to read books and to draw pictures" - ?

 

You should post more examples and try to define the rules of words to look for.

 

inavjots
Fluorite | Level 6
The question has been updated. Please take a look now. I hope it is understandable now.
Shmuel
Garnet | Level 18

You gave only one example and did not relate to the example I have posted.

 

For your specific example you can use next code, though it's not a complete, because

you will get a bad result for the 2nd example. Check output.

data test;
   infile cards truncover;
   input hobbies $char50. ;
cards;
Reading and Singing
I like to read books and to draw pictures
;
run;
data want;
 set test;
    do i=1 to countw(hobbies);
       if scan(lowcase(hobbies),i) in ('and', 'or') then do;
          hobby1 = scan(hobbies, i-1);
          hobby2 = scan(hobbies, i+1);
          output;
       end;
    end;
run;

To get a better code you need to post more examples with the desired output.

 

Assuming hobbies are usually verbs, it may be more difficult to decide by a program which word is a verb.

inavjots
Fluorite | Level 6

This is perfect. Much obliged.

Reeza
Super User
You can use SCAN() and FINDW()

Use FINDW() to see if you have an AND/OR and then if you do, use SCAN() to portion them out. Or use SCAN to loop through the words and drop any and/or's.

inavjots
Fluorite | Level 6
Thank you for your reply, Could you please explain it with an example? I am not familiar with FINDW().
Reeza
Super User
This is the closest example I have. You can add conditions to it.
https://gist.github.com/statgeek/bed5ea2c12903b38fdcf19f3f1f1aae9

If you need further assistance please show what you've tried and what issues you're having.

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
  • 7 replies
  • 805 views
  • 5 likes
  • 3 in conversation