DATA Step, Macro, Functions and more

Having Trouble in by group processing

Reply
Regular Learner
Posts: 1

Having Trouble in by group processing

Hi,

 

I am trying to sum up observations by group processing but after writing below code it's only giving me below the last line as output.

 

libname a "&path";

proc sort data = a.data out=a.data1; by CTYPE; Run; data a.processing(keep=Sales _Other); set a.data1; by CTYPE; if first.CTYPE then Sales=0; Sales + _Other; if last.CTYPE; Run; proc print data = a.processing; run;

after running code, there was only last line number of group produced as output.

 

 

 

Thanks.

PROC Star
Posts: 1,400

Re: Having Trouble in by group processing

Posted in reply to gsavaliya92

How many levels of CTYPE do you have? Show ud some of your data and how your output data differ from what you expect.

 

Makes it much easier to help you Smiley Happy

Respected Advisor
Posts: 3,275

Re: Having Trouble in by group processing

[ Edited ]
Posted in reply to gsavaliya92

Instead of using your own code (which can be a problem to write and fix bugs) to do this, you can get the information you need out of PROC MEANS or PROC SUMMARY. Since SAS has already done the work to program this type of summing over groups, there's no need for you to re-invent how to do it.

--
Paige Miller
Super User
Posts: 13,941

Re: Having Trouble in by group processing

Posted in reply to gsavaliya92

@gsavaliya92 wrote:

Hi,

 

I am trying to sum up observations by group processing but after writing below code it's only giving me below the last line as output.

 

libname a "&path";

proc sort data = a.data out=a.data1; by CTYPE; Run; data a.processing(keep=Sales _Other); set a.data1; by CTYPE; if first.CTYPE then Sales=0; Sales + _Other; if last.CTYPE; Run; proc print data = a.processing; run;

after running code, there was only last line number of group produced as output.

 

 

 

Thanks.


Your line of code

if last.CTYPE;

Says "if this is the last value of the ctype group then output the record". If you want all records remove that line.

 

 

Super User
Posts: 6,933

Re: Having Trouble in by group processing

Posted in reply to gsavaliya92

You're getting that result, because that's what the program is designed to do.

 

There are several signs within the program that indicate you don't understand that this is what the program does.  If the program is designed to keep only one summary observation per CTYPE:

 

  • Why not keep CTYPE as one of the variables being output?
  • Why keep _OTHER since it is merely the last value being added into the total?

Bottom line:  the program doesn't do what you want.  It's not supposed to.  But you need to describe what you do want, if you would like some help.

Ask a Question
Discussion stats
  • 4 replies
  • 103 views
  • 0 likes
  • 5 in conversation