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.
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 🙂
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.
@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.
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:
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.