Hello,
I have a field which appears in this format: XXXX YYYY ZZZZ
Where it is separated by space. I would like to make a new line per space delimited string. So for the above example there would be 3 lines create.
Any help would be appreciate?
Thanks.
Please try
data want;
set have;
count=countw(variable,' ');
do i = 1 to count;
new=scan(variable,i,' ');
output;
end;
run;
can you please show how output should look like
Hello,
This is a basic structure which I am looking for:
Before:
id | Variable |
1 | XXXX YYYY ZZZZ |
2 | AAAA BBBB |
After:
id | new_variable |
1 | XXXX |
1 | YYYY |
1 | ZZZZ |
2 | AAAA |
2 | BBBB |
Thanks
Please try
data want;
set have;
count=countw(variable,' ');
do i = 1 to count;
new=scan(variable,i,' ');
output;
end;
run;
COUNTW to count the number of words
SCAN to extract the number of components.
n_words = countw(string);
do i=1 to n_words;
word = scan(string, i);
OUTPUT;
end;
A fully worked example is here:
https://gist.github.com/statgeek/bed5ea2c12903b38fdcf19f3f1f1aae9
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.