BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
data ds;
input values;
datalines;
-9
-6
3
4
-7
-1
-6
7
6
;
run;

How to count and sum positive and negative values 

2 REPLIES 2
mkeintz
PROC Star

I don't propose to provide code for you, but here's how I would think about it.

 

  1. I'd run a DATA step, which reads the data one row at a time
  2. In the data step, I'd keep two running totals, one for the positive and the other for the negative
    1. This means I'd have to compare each incoming VALUES to zero and depending on the result, update the appropriate sum.
    2. It also means that the variable holding the running sums have to be retained over a sequence of rows - i.e. not reset to missing.
  3. And I'd have to detect when I'm processing the last row, so as to output the results.

 

You can also run something like proc means twice, once on the subset of positive values, and once on the negative subset.

 

Editted note:  Finally, and most importantly, you can show what you tried (i.e. show us your SAS log), and you'll no doubt get helpful answers.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Patrick
Opal | Level 21

@BrahmanandaRao 

This forum is about SAS users supporting other SAS users.

What's appreciated is if you demonstrate some own effort as else you might end-up with people no more providing answers to your questions.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 469 views
  • 0 likes
  • 3 in conversation