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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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