Help using Base SAS procedures

Grouping Observations

Reply
Super Contributor
Posts: 268

Grouping Observations

Name                    city                         county                 

Ann                        A                             1                             

Betty                      B                             2                             

Carol                      A                             1                             

Dave                      B                             2                             

Ethel                      A                             2                             

I want to output a file that groups by county and then city and write those obs on one line with the names listed that are associated with that city and county, like so:

Name                    city                         county

Ann, Carol           A                             1

Betty, Dave         B                             2

Ethel                    A                             2

Super Contributor
Posts: 1,636

Re: Grouping Observations

data have;
input Name :$                   city :$                  county;
cards;
Ann                        A                             1                             
Betty                      B                             2                             
Carol                      A                             1                             
Dave                      B                             2                             
Ethel                      A                             2
;
proc sort;
by county city name;

data want;
retain new;
length new  $50;
set have;
by county city;
new=catx(',',new,name);
if last.city then do;
output;
call missing (new);
end;
proc print;run;

Respected Advisor
Posts: 3,124

Re: Grouping Observations

Or using DOW:

data have;

infile cards;

input (Name                    city                         county) (:$);

cards;

Ann                        A                             1                            

Betty                      B                             2                            

Carol                      A                             1                            

Dave                      B                             2                            

Ethel                      A                             2    

;

proc sort data=have;

by county city;

run;

data want;

length group $40;

do until (last.city);

set have;

by county city;

group=catx(',',group,name);

end;

run;

proc print;run;

Haikuo

Ask a Question
Discussion stats
  • 2 replies
  • 118 views
  • 0 likes
  • 3 in conversation