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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.