How to make the code work in rolling window analysis

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 130
Accepted Solution

How to make the code work in rolling window analysis

The following code is provided by data_null__ for my prior question. the code works perfect when there do exist empty datasets.

 

Then I tried to apply it to rolling window regression. some rolling windows' regressions generate empty tables, while other rolling windows' regressions do not generate empty tables.  The SAS program stop in the rolling window where there is no empty table.

 

Anyway to force the program to continue processing when there is no empty table? Thanks a lot!

 

 

proc sql noprint; 
   select memname into :zero separated by ' '
      from dictionary.tables 
      where libname eq 'WORK' and memname eqt 'HAVE_' and nobs eq 0; 
   quit; 
   run; 
%put NOTE: &=zero;
data &zero;
   do _n_ = 1 to 3; 
      output; 
      end; 
   stop; 
   set &zero;
   run; 

 


Accepted Solutions
Solution
‎12-10-2015 11:24 AM
Respected Advisor
Posts: 3,777

Re: How to make the code work in rolling window analysis

I believe all you need to do is assign ZERO=_NULL_ before calling PROC SQL.  PROC SQL does not create and INTO variable when "No rows were selected";  Assignin _NULL_ will allow the data step to execute without error but do nothing.

24         %let zero=_NULL_;
25         proc sql noprint;
26            select memname into :zero separated by ' '
27               from dictionary.tables
28               where libname eq 'WORK' and memname eqt 'HAVE_' and nobs eq 0;
NOTE: No rows were selected.
29            quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.03 seconds
      cpu time            0.00 seconds
      

30            run;
31         %put NOTE: &=zero;
NOTE: ZERO=_NULL_
32         data &zero;
33            do _n_ = 1 to 3;
34               output;
35               end;
36            stop;
37            set &zero;
38            run;

NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

View solution in original post


All Replies
Super User
Super User
Posts: 7,401

Re: How to make the code work in rolling window analysis

Do you mean the file does not exist at all?   If so then use the exist() function:

data _null_;

  if %sysfunc(exist(<libname>.<dataset>))=0 then call execute('proc sql; create table <libname>.<dataset> (a num); quit;');

...

run;

 

The above will check if <libname>.<dataset> exists and if not create it with a variable a and no records.  Then it will exist for the =0 calculation.

 

To be honest though, if your previous code is not generating what you want, then I would look at the process you are doing, and see if you can create logic which does work as intended - see my reply to your other post - there is no need to split things out or create lists, it can all be done in base SAS with varying structures.  Otherwise you end up creating other problems like this.

 

Frequent Contributor
Posts: 130

Re: How to make the code work in rolling window analysis

Thank you for your advice. since the original program is wrote in that way, it will take a lot of effort to rewrit it. so I thought it would save my time to just "fixed" it. I agree with you that if I write a code from sketch I should start with a "simple" way from every beginning.

Solution
‎12-10-2015 11:24 AM
Respected Advisor
Posts: 3,777

Re: How to make the code work in rolling window analysis

I believe all you need to do is assign ZERO=_NULL_ before calling PROC SQL.  PROC SQL does not create and INTO variable when "No rows were selected";  Assignin _NULL_ will allow the data step to execute without error but do nothing.

24         %let zero=_NULL_;
25         proc sql noprint;
26            select memname into :zero separated by ' '
27               from dictionary.tables
28               where libname eq 'WORK' and memname eqt 'HAVE_' and nobs eq 0;
NOTE: No rows were selected.
29            quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.03 seconds
      cpu time            0.00 seconds
      

30            run;
31         %put NOTE: &=zero;
NOTE: ZERO=_NULL_
32         data &zero;
33            do _n_ = 1 to 3;
34               output;
35               end;
36            stop;
37            set &zero;
38            run;

NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
Frequent Contributor
Posts: 130

Re: How to make the code work in rolling window analysis

Thanks a lot!

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 279 views
  • 2 likes
  • 3 in conversation