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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 10223 views
  • 2 likes
  • 4 in conversation