DATA Step, Macro, Functions and more

sum vertically

Frequent Contributor
Posts: 76

sum vertically

Hii ,I have a data set shown below


sex    count

f          122

m        100 


I need the following output

sex    count

f           122

m         100    

t           222


this has to be done in data step only ,don't use procedures

Super User
Posts: 23,663

Re: sum vertically

Look up the RETAIN statement. 

Trusted Advisor
Posts: 1,837

Re: sum vertically

Use RETAIN total and add the count to it at every observation.

Assign a variable to set input with end=     (like end=eof)

and next lines to your code:

      IF eof then do;

         sex = 'TOTAL';

         count = total;



Super User
Posts: 6,751

Re: sum vertically

Just looking at the elephant from a slightly different viewpoint:


data want (drop=totcount);

do until (done);

   set have end=done;


   totcount + count;



count = totcount;



Super User
Posts: 13,498

Re: sum vertically

Please be aware that the habit of putting a summary value into a data set unless strictly for the purpose of display (in which case a report procedure may be more appropriate for many other reasons) is likely to cause problems.


For instance if you were to take your resulting data set into a regression type analysis you now have 3 levels of "sex" and one of them has way larger values than the other. Results could be quite entertaining... Worse would be an analysis that does not consider Sex but just uses the count.

Ask a Question
Discussion stats
  • 4 replies
  • 5 in conversation