BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
2 REPLIES 2
chang_y_chung_hotmail_com
Obsidian | Level 7
> 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


   */

deleted_user
Not applicable
Chang, thank you for the information. Will update or close this record soon.

Regards,

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1525 views
  • 0 likes
  • 2 in conversation