BookmarkSubscribeRSS Feed
gsavaliya92
Calcite | Level 5

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.

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

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 🙂

PaigeMiller
Diamond | Level 26

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
ballardw
Super User

@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.

 

 

Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 661 views
  • 0 likes
  • 5 in conversation