BookmarkSubscribeRSS Feed
hellohere
Pyrite | Level 9

Try to CONCAN string into a line. Oracle has group_concan(). How to do it in SAS/SQL? Anyone

 

proc sql;
create table summ_peek as
select case when cylinders>=6 then 1 else 0 end as flag, count(*) as cttot, concan(make) as make
from sashelp.cars
group by 1;
quit;

5 REPLIES 5
Reeza
Super User

Here are two ways:

https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a

 

EDIT: That SQL will remerge with original table though, giving you 1 record per observation as the first calculation is row level, not an aggregate calculation. 

ORACLE would error out I believe. 

 


@hellohere wrote:

Try to CONCAN string into a line. Oracle has group_concan(). How to do it in SAS/SQL? Anyone

 

proc sql;
create table summ_peek as
select case when cylinders>=6 then 1 else 0 end as flag, count(*) as cttot, concan(make) as make
from sashelp.cars
group by 1;
quit;


 

 

hellohere
Pyrite | Level 9

Do you mind to Cut-&-Paste the webpage( text only is fine)? I cannot access that webpage. Thanks 

Reeza
Super User
*create sample data for demonstration;
data have;
    infile cards dlm='09'x;
    input OrgID Product $   States $;
    cards;
1   football    DC
1   football    VA
1   football    MD
2   football    CA
3   football    NV
3   football    CA
;
run;

*Sort - required for both options;
proc sort data=have;
    by orgID;
run;

**********************************************************************;
*Use RETAIN and BY group processing to combine the information;
**********************************************************************;
data want_option1;
    set have;
    by orgID;
    length combined $100.;
    retain combined;

    if first.orgID then
        combined=states;
    else
        combined=catx(', ', combined, states);

    if last.orgID then
        output;
run;

**********************************************************************;
*Transpose it to a wide format and then combine into a single field;
**********************************************************************;
proc transpose data=have out=wide prefix=state_;
    by orgID;
    var states;
run;

data want_option2;
    set wide;
    length combined $100.;
    combined=catx(', ', of state_:);
run;
hellohere
Pyrite | Level 9
thanks
s_lassen
Meteorite | Level 14

The first solution suggested by @Reeza can be made a bit simpler (and slightly faster) like this:

data want_option1;
  do until(last.orgID);   
    set have;
    by orgID;
    length combined $100.;
    call catx(', ', combined, states);
end; run;

And it has to be done in a data step, because SAS SQL does not have concatenation as a "summary" function, unlike most other SQL dialects.

 

If performance matters, you should always use CALL CATX rather than the function.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 560 views
  • 6 likes
  • 3 in conversation