BookmarkSubscribeRSS Feed
molla
Fluorite | Level 6

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

4 REPLIES 4
Reeza
Super User

Look up the RETAIN statement. 

Shmuel
Garnet | Level 18

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;

         output;

    end;

Astounding
PROC Star

Just looking at the elephant from a slightly different viewpoint:

 

data want (drop=totcount);

do until (done);

   set have end=done;

   output;

   totcount + count;

end;

sex='t';

count = totcount;

output;

run;

ballardw
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 774 views
  • 4 likes
  • 5 in conversation