DATA Step, Macro, Functions and more

how to calculate sum and replace it in missing value

Accepted Solution Solved
Reply
Occasional Contributor mj5
Occasional Contributor
Posts: 10
Accepted Solution

how to calculate sum and replace it in missing value

I have a dataset as below

PT     N

102   25

103   34

Total   .

 

I need to replace the missing with the sum of the 2 counts in N. Can i do that in datastep?

 


Accepted Solutions
Solution
‎10-20-2017 05:39 AM
Super User
Posts: 7,809

Re: how to calculate sum and replace it in missing value

You can retain a variable where you sum up all N values, and when PT = 'Total', you set N to that value.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎10-20-2017 05:39 AM
Super User
Posts: 7,809

Re: how to calculate sum and replace it in missing value

You can retain a variable where you sum up all N values, and when PT = 'Total', you set N to that value.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,970

Re: how to calculate sum and replace it in missing value

Whilst @KurtBremser has given you the answer, I would step back one and ask why you have a dataset which looks like that in the first place.  How have you added "Total" without adding the sum in at the same time, seems like the step before needs changing.  You can add totals on in various ways, my first thought was:

proc sql;
  create table WANT as
  select  *
  from    HAVE
  union all
  select  "Total",
          sum(N) 
  from    HAVE;
quit;

Simply adds a row at the end of the dataset with total.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 103 views
  • 1 like
  • 3 in conversation