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

Is there a way in PROC SQL to split a string that contains commas so that the end result is displayed as a single value on each row?

 

example:

 

Variable1

1111,2222,3333,4444,

6666,7777,8888,

5555,9999

 

Need to return as:

Variable1

1111

2222

3333

4444

6666

7777

8888

5555

9999

 

I tried using a scan function, however have not had much luck.  Any help would be greatly appreciated.  Thank you in advance.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User
data have ;
  var1 = '1111,2222,3333,4444,6666,7777,8888,5555,9999';
run;

Easy enough with a data step.

data want ;
  set have;
  save=var1 ;
  do i=1 to countw(save,',');
     var1=scan(save,i,',');
     output;
  end;
  drop i save ;
run;

Pain in the neck in PROC SQL.  What is the upper limit on the number of values that could be in the variable? You need to code for all of them.  

proc sql;
  create table want as 
  select scan(var1,1,',') as var1 from have 
  union select scan(var1,2,',') as var1 from have where calculated var1 is not null
  union select scan(var1,3,',') as var1 from have where calculated var1 is not null
  ....
  ;
quit;

View solution in original post

5 REPLIES 5
collinelliot
Barite | Level 11

Do you need to do this in proc sql?

PaigeMiller
Diamond | Level 26

This would be very easy in a data step, and as far as I know, difficult if not impossible in PROC SQL. Your choice.

--
Paige Miller
Tom
Super User Tom
Super User
data have ;
  var1 = '1111,2222,3333,4444,6666,7777,8888,5555,9999';
run;

Easy enough with a data step.

data want ;
  set have;
  save=var1 ;
  do i=1 to countw(save,',');
     var1=scan(save,i,',');
     output;
  end;
  drop i save ;
run;

Pain in the neck in PROC SQL.  What is the upper limit on the number of values that could be in the variable? You need to code for all of them.  

proc sql;
  create table want as 
  select scan(var1,1,',') as var1 from have 
  union select scan(var1,2,',') as var1 from have where calculated var1 is not null
  union select scan(var1,3,',') as var1 from have where calculated var1 is not null
  ....
  ;
quit;
collinelliot
Barite | Level 11

Nice illustration that it certainly can be done in SQL, but it will get really burdensome with a lot of delimited values. This also assumes that there aren't many other variables to deal with, so I'm really wondering why it has to be done in SQL?

PhatRam33
Fluorite | Level 6
Thank you very much. Both solutions worked like a charm, however the data step is indeed the much easier option. Thanks again!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 38388 views
  • 2 likes
  • 4 in conversation