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
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.