BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
WestChan
Fluorite | Level 6

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

Please try

 

data want;
set have;
count=countw(variable,' ');
do i = 1 to count;
new=scan(variable,i,' ');
output;
end;
run;

Thanks,
Jag

View solution in original post

4 REPLIES 4
kiranv_
Rhodochrosite | Level 12

can you please show how output should look like

WestChan
Fluorite | Level 6

Hello, 

 

This is a basic structure which I am looking for: 

 


Before: 

idVariable
1XXXX YYYY ZZZZ
2AAAA BBBB

 

 

After: 

 

idnew_variable
1XXXX
1YYYY
1ZZZZ
2AAAA
2BBBB

 

Thanks

Jagadishkatam
Amethyst | Level 16

Please try

 

data want;
set have;
count=countw(variable,' ');
do i = 1 to count;
new=scan(variable,i,' ');
output;
end;
run;

Thanks,
Jag
Reeza
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 10729 views
  • 2 likes
  • 4 in conversation