Programming the statistical procedures from SAS

Dow Loop-Basics

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

Dow Loop-Basics

Hi TEam,

I was reading a paper on Dow Loops.

http://analytics.ncsu.edu/sesug/2010/BB13.Dorfman.pdf

The example is from page 2 of this paper.

"If VAR is missing CONTINUE passes the control straight to the bottom of the loop".

What is the meaning of bottom of the loop? Is it not gona calculate Mcount Prod and SUm for that variable and goes to the next variable but still with in the loop?Is that the meaning ?

Secondly,

"PROD and COUNT are

set to 1, and the non-retained SUM, MEAN, and MCOUNT are set to missing by the default action of the

implied loop (program control at the top of the implied loop)."

This sentence says that SUM Mean and Count are set to missing . But what lines in the code sets these values to missing??

Any help is greatly appreciated possibly with an example would be great

Thanks

Data B ( Keep = Id Prod Sum Count Mean) ;

  Prod = 1 ;

   Do Count = 1 By 1 Until ( Last.Id ) ;

   Set A ;

    By Id ;

       If Missing (Var) Then Continue ;

      Mcount = Sum (Mcount, 1) ;

       Prod = Prod * Var ;

       Sum = Sum (Sum, Var) ;

       End ;

       Mean = Sum / Mcount ;

Run ;

Message was edited by: Karun Diri Also I feel the keep statement would have been good with the set statement rather than the Data B Any suggestions??


Accepted Solutions
Solution
‎10-05-2012 03:04 PM
SAS Super FREQ
Posts: 3,556

Re: Dow Loop-Basics

MCOUNT = mean count = nonmissing values.

If I may offer some advice, I think you are struggling with the abstractions presented by macros and advanced SUGI papers that aren't necessary to master until you gain more experience.  I know people who have been SAS programmers for years and who have never written a DOW loop.  When I was first learning SAS, I liked to experiment with SAS on data that was small, concrete, and familiar.  I suggest you run and study this concrete example. And when the next question comes up, see if you can construct a simple example that uses the Sashelp.Class data set. It is the "best friend" of discussion forums because it enables everyone to run the example and to speak in terms of concrete variables and observations.

/* sort data to enable BY group analysis */
proc sort data=sashelp.class out=class;
by sex;
run;

title "Original data";
proc print data=Class; run;

/* compute mean as an example of a DOW loop */
Data B ( Keep = Sex MCountSum Mean) ;
   Do Until ( Last.Sex ) ;
   Set Class ;
   By Sex;
      If Missing (Age) Then Continue ;
      MCount = Sum (MCount, 1) ;
      Sum = Sum (Sum, Age) ;
      put _ALL_; /* look in the SAS LOG to see each iteration */
   End;
   Mean = Sum / MCount;
Run ;

title "Computed results";
proc print data=B; run;

/* compare result to standard call to PROC MEANS */
title "Results of PROC MEANS";
proc means data=class Sum Mean;
class sex;
var age;
run;

View solution in original post


All Replies
SAS Super FREQ
Posts: 3,556

Re: Dow Loop-Basics

Karun,

I'm glad you are learning about SAS. Did you know that there is a SAS Community forum dedicated to the DATA step, macros, and other questions that you've been asking? https://communities.sas.com/community/support-communities/sas_macro_facility_data_step_and_sas_langu...

I think that your questions are better suited for that forum because (1) there are more experts there, and (2) the readers of those forums can learn from your questions and from the posted answers.

The forum for Statistical Procedures is primarily intended for questions about procedures in SAS/STAT such as PROC REG, GLM, LOGISTIC, PRINCOMP, and so forth.

Best wishes for successful learning,

Rick

Super Contributor
Posts: 1,040

Re: Dow Loop-Basics

Thanks Rick,

I didnt know about that. Infact i was confused. When you first directed me to use SAS almost 3 months ago u posted a link and I was blindly following to post questions on that forum...I will explore a little bit..

Thanks for the help

SAS Super FREQ
Posts: 3,556

Re: Dow Loop-Basics

Yeah, I kind of figured that. I should have sent you to the main page. Bookmark this URL and then pick the sub-forum that fits the question you want to ask: https://communities.sas.com/community/support-communities

BTW: You can't use the KEEP statement on the SET statement because those variables (except ID) don't live in the A data set. They are being manufactured in the current DATA step from the Var variable (which does live in A).

Super Contributor
Posts: 1,040

Re: Dow Loop-Basics

Got you.

1)I am confused with what  MCOUNT does?does  it stand for missing count??

2)if mising (var) then continue

he says that continue passes control to the bottom of the loop.

Bottom means he is not wanting to calculate the MCOUNT PROD AND SUM if a variable has missing value? Is that true???

Thanks

Solution
‎10-05-2012 03:04 PM
SAS Super FREQ
Posts: 3,556

Re: Dow Loop-Basics

MCOUNT = mean count = nonmissing values.

If I may offer some advice, I think you are struggling with the abstractions presented by macros and advanced SUGI papers that aren't necessary to master until you gain more experience.  I know people who have been SAS programmers for years and who have never written a DOW loop.  When I was first learning SAS, I liked to experiment with SAS on data that was small, concrete, and familiar.  I suggest you run and study this concrete example. And when the next question comes up, see if you can construct a simple example that uses the Sashelp.Class data set. It is the "best friend" of discussion forums because it enables everyone to run the example and to speak in terms of concrete variables and observations.

/* sort data to enable BY group analysis */
proc sort data=sashelp.class out=class;
by sex;
run;

title "Original data";
proc print data=Class; run;

/* compute mean as an example of a DOW loop */
Data B ( Keep = Sex MCountSum Mean) ;
   Do Until ( Last.Sex ) ;
   Set Class ;
   By Sex;
      If Missing (Age) Then Continue ;
      MCount = Sum (MCount, 1) ;
      Sum = Sum (Sum, Age) ;
      put _ALL_; /* look in the SAS LOG to see each iteration */
   End;
   Mean = Sum / MCount;
Run ;

title "Computed results";
proc print data=B; run;

/* compare result to standard call to PROC MEANS */
title "Results of PROC MEANS";
proc means data=class Sum Mean;
class sex;
var age;
run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 316 views
  • 3 likes
  • 2 in conversation