Help using Base SAS procedures

total and average values across observations using the data step

Reply
N/A
Posts: 0

total and average values across observations using the data step

We have a simple SAS program in which we want to loop through the observations twice.
The first time we will add up a column across all the rows and get the total and average value.
For the second loop we will compare the value from the specific row against the total and/or average mentioned above and make a decision to take certain actions for the specific row.
So the questions may be:
1. How do we loop through more than once (using the SET statement) in a data step? Or
2. Can we pass a value (the total/average) from one data step to another data step? That is, one loop through per data step using the SET statement. Or
3. Is there an easy way to obtain the total/average value across all of the observations?

Thank you so much if someone can shed light on this question.

JC
Regular Contributor
Posts: 241

Re: total and average values across observations using the data step

Posted in reply to deleted_user
> 1. How do we loop through more than once (using the SET statement) in a data step?

By reading the input dataset more than once using set statement(s).



> 2. Can we pass a value (the total/average) from one data step to another data step?

Yes, we can.



> 3. Is there an easy way to obtain the total/average value across all of the observations?

Yes, there is.



But this is much easier with proc sql, since unlike other sql implementations, it automatically merges back the summary stats. Below assuming no missing values:



   proc sql;


     select name, age, mean(age) as meanAge


          , ifc(age<(calculated meanAge), "below""at or above"as label


     from   sashelp.class


     where  name like "A%";


   quit;


   /* on lst


   Name           Age   meanAge  label


   -----------------------------------------


   Alfred          14      13.5  at or above


   Alice           13      13.5  below


   */

N/A
Posts: 0

Re: total and average values across observations using the data step

Posted in reply to chang_y_chung_hotmail_com
Chang, thank you for the information. Will update or close this record soon.

Regards,
Ask a Question
Discussion stats
  • 2 replies
  • 245 views
  • 0 likes
  • 2 in conversation