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.

CFC_SAS_Communities_400x225.jpgCFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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