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
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.
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.
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.
This is perfect. Much obliged.
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.
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.