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

I am using Listagg function in SAS, Proc sql;(Connected to oracle)- I connect to Oracle and this function works.

I need help in adding comma's for 2 columns. (either Listagg or By process will work)

My current code picks only works for col1.

 

LISTAGG(col1, ',') WITHIN GROUP (ORDER BY id) AS flag1

 

Given data

 

ID         col1           colb

1          blue            US   

1          green          US

1          pink             US

2          blue             US

2          pink             BR

2          green           CA

 

Want data: (I need Flag1 or Flag2 or both . Flag2 is better for me)

 

ID   Flag1                                                                 Flag 2

1    ('blue','US', 'green','US','pink','US')                 ('blue-US','green-US','pink-US')

2    ('blue','US','pink','BR','green','CA')                  ('blue-US','pink-BR','green-CA')

 

My current data: (Listagg function)

 

ID  flag1

1   ('blue','green',pink')

 

 

I can also try the samething using By statement process, but only works for one column.

I need Flag1 or Flag2 or both

 

Proc sort data= have;
By custId;
Run;

Data want;

Set have;
By ID;
Length list $500.;Retain List;

If first.ID then list= variable;
Else list = catx(', ', list, variable);

If last.ID then output;
Run;

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

You can do it by using the || (concatenation) operator in Oracle SQL - just a minimal change of your initial code. e.g.:

LISTAGG(col1||'-'||colb, ',') WITHIN GROUP (ORDER BY id) AS flag2

View solution in original post

5 REPLIES 5
mkeintz
PROC Star

@Kalai2008 wrote:

I am using Listagg function in SAS, Proc sql;(Connected to oracle)- I connect to Oracle and this function works.

... stuff deleted... (bold italics below are mine):

 

I can also try the same thing using By statement process, but only works for one column.

I need Flag1 or Flag2 or both

 

Proc sort data= have;
By custId;
Run;

Data want;

Set have;
By ID;
Length list $500.;Retain List;

If first.ID then list= variable;
Else list = catx(', ', list, variable);

If last.ID then output;
Run;


Why do you think it "works only for one column"?  Couldn't you do something like this?

 

 

data want;
   set have;
   by id;
   length list1  list2 $200;
   retain list1 list2 ;
   if first.id then call missing(list1,list2);
   list1=catx(',',list1,var1);
   list2=catx(',',list2,var2);
   if last.id then output;
run;

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Kalai2008
Pyrite | Level 9
Thanks for checking, Yes, I can still do for 2 columns separately. Please check MY WANT data, I wanted to combine col1 and col2, get results in one column. My data is huge, around 20 million records, so better to use listagg function.
mkeintz
PROC Star

I presume you think the listagg function is "better" because it is an in-database function.

 

But if you need to do it in the sas data step:

 

data have;
  input ID col1 :$5. colb :$2.;
datalines;
1 blue   US   
1 green  US
1 pink   US
2 blue   US
2 pink   BR
2 green  CA
run;
data want;
  set have;
  by id;
  length flag2 $100;
  retain flag2;
  if first.id then call missing(flag2);
  flag2=catx(',',flag2,quote(catx('-',col1,colb),"'"));
  if last.id;
  flag2=cats('(',flag2,')');
  put  flag2=;
run;

This just nests a catx function inside a quote function inside a catx to produce flag2.

 

I presume that Oracle allows you to do similar nesting of quoting and within-row concatenating inside the listagg function.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Kalai2008
Pyrite | Level 9
Thank you and it worked.
s_lassen
Meteorite | Level 14

You can do it by using the || (concatenation) operator in Oracle SQL - just a minimal change of your initial code. e.g.:

LISTAGG(col1||'-'||colb, ',') WITHIN GROUP (ORDER BY id) AS flag2

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 2799 views
  • 3 likes
  • 3 in conversation