DATA Step, Macro, Functions and more

Display all observations from first. last. by grouping

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Display all observations from first. last. by grouping

Hi,

 

I am merging two data sets and I am creating two Vars based on the first. last. by groups. Everything is merging fine, the newly created Vars and totaling fine, but my output is only displaying the totals or rather the final observation from each by group. I would like all the observations within each by group displayed in addition to the total and I know there is a way to do this but I can not find it in my notes or online anywhere.

 

My code is pretty basic:

Data set;

merge one two;

by animal name age;

 

if first.age then do;

        totpounds=0;

end;

totpounds+pounds;

if last.age output;

 

run;

 

These are dummy Vars but basically that's the idea. Any help would truly be appreciated.

 

Thank you!


Accepted Solutions
Solution
‎12-05-2017 10:27 AM
Super User
Super User
Posts: 9,227

Re: Display all observations from first. last. by grouping

What do you mean by total on each observation, do you mean incremental, or sum() all on every line?  For incremental you can do it in the one step:

data want;    
  merge one two;
  by animal name age;
  totpounds=ifn(first.age,pounds,totpounds+pounds);
run;

If you want the total on each row, then do that in a separate step and merge back e.g:

proc sql;
  create WANT as
  select   A.*,
           B.TOT
  from     HAVE A
  left join (select AGE,sum(POUNDS) as TOT from HAVE group by AGE) B
  on        A.AGE=B.AGE;
quit;

 

View solution in original post


All Replies
Respected Advisor
Posts: 2,660

Re: Display all observations from first. last. by grouping


kmj636 wrote:

Hi,

 

... but my output is only displaying the totals or rather the final observation from each by group ...

That is happening because you are telling SAS to only ouptut the final observation from each group.

 

If you take out the statement

 

if last.age then output;

then it won't happen.

--
Paige Miller
Contributor
Posts: 24

Re: Display all observations from first. last. by grouping

Posted in reply to PaigeMiller

I really apologize and appreciate your view and time. Yes, it should be and is "then output."

I am still not able to see the contributing observations to the totals from the by groups.

Respected Advisor
Posts: 2,660

Re: Display all observations from first. last. by grouping


kmj636 wrote:

I really apologize and appreciate your view and time. Yes, it should be and is "then output."

I am still not able to see the contributing observations to the totals from the by groups.


Even though your code was incorrect and omitted the word "then", if you remove this line entirely, you will see the contributing observations.

--
Paige Miller
Solution
‎12-05-2017 10:27 AM
Super User
Super User
Posts: 9,227

Re: Display all observations from first. last. by grouping

What do you mean by total on each observation, do you mean incremental, or sum() all on every line?  For incremental you can do it in the one step:

data want;    
  merge one two;
  by animal name age;
  totpounds=ifn(first.age,pounds,totpounds+pounds);
run;

If you want the total on each row, then do that in a separate step and merge back e.g:

proc sql;
  create WANT as
  select   A.*,
           B.TOT
  from     HAVE A
  left join (select AGE,sum(POUNDS) as TOT from HAVE group by AGE) B
  on        A.AGE=B.AGE;
quit;

 

Contributor
Posts: 24

Re: Display all observations from first. last. by grouping

You are wonderful! Thank you so much.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 115 views
  • 0 likes
  • 3 in conversation