DATA Step, Macro, Functions and more

Can't get a simply BY grouping to work. Please help!

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Can't get a simply BY grouping to work. Please help!

Hello I havent used SAS in a while, and want to attain a basic programmer cert.

 

 

 

I'm trying to get this grouping to work from sashelp.cars but it is giving me all 128 observations in the output. I see repeated valuesfor the BY variable make

 

proc sort data=sashelp.cars out=work.temp2 (keep = make);
by make;
run;
proc print data = work.temp2;

 

 

any suggestions?


Accepted Solutions
Solution
‎11-24-2016 09:57 AM
Super User
Super User
Posts: 7,401

Re: Can't get a simply BY grouping to work. Please help!

The option is nodupkey.

If your familiar with SQL then use proc sql:

proc sql;
  create table WANT as
  select  MAKE,
          sum(MSRP) as RES
  from    SASHELP.CARS
  group by MAKE;
quit;

View solution in original post


All Replies
Super User
Super User
Posts: 7,401

Re: Can't get a simply BY grouping to work. Please help!

Yes, by group just means to do something for each group specified, it doesn't remove any variables.  Perhaps you mean to sort the dataset using nodupkey:

proc sort data=sashelp.cars out=work.temp2 (keep = make) nodupkey;
  by make;
run;
proc print data = work.temp2;
run;

The nodupkey options will remove any subsequent observations from each by group.

Occasional Contributor
Posts: 5

Re: Can't get a simply BY grouping to work. Please help!

thanks so much-

 

If I wanted to sum msrp against Make grouping how would I go about that?

 

my code below is still giving me all 428 obs in the output.

 

proc sort data=sashelp.cars out=work.temp2 (keep = make msrp);
by make;
run;
proc print data = work.temp2;
sum msrp;
run;

Occasional Contributor
Posts: 5

Re: Can't get a simply BY grouping to work. Please help!

this seemed to work.

proc sort data=sashelp.cars out=work.temp2 (keep = make msrp) nodupekey;
by make;
run;
proc print data = work.temp2;
sum msrp;
run;

coming from the world of sql I was really looking for a way to just group the output and supress repeated by variables as opposed to supressing them in the output data itself.

Is there a way to do that?
Solution
‎11-24-2016 09:57 AM
Super User
Super User
Posts: 7,401

Re: Can't get a simply BY grouping to work. Please help!

The option is nodupkey.

If your familiar with SQL then use proc sql:

proc sql;
  create table WANT as
  select  MAKE,
          sum(MSRP) as RES
  from    SASHELP.CARS
  group by MAKE;
quit;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 181 views
  • 2 likes
  • 2 in conversation