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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.