BookmarkSubscribeRSS Feed
SRINIVAS_N
Calcite | Level 5

 

Hi friends,

 

I need some help to complete one task.

There is a group of datasets in which I need to search for particular  datasets ,if the dataset is found then the name gets displayed and in case if the dataset is not found it has to search in a time period of three hours for every 15  minutes.It means for every 15th min the loop should start searching for the datatset continuously till three hours.  And if the dataset is not found the loop must terminate and the execution should stop showing the error message in the error log.

 

I have a ssample code where the loop is getting executed but not showing the error message in error log.Please suggest some changes.

 

sample :

 

%macro checkds(dsn,time_to_check_in_sec=60,check_interval_in_sec=30);

%local startdttm;
%let startdttm=%sysfunc(datetime());
%do %while( %sysfunc(sum(%sysfunc(datetime()),-&startdttm))<&time_to_check_in_sec );
%if %sysfunc(exist(&dsn)) %then
%do;
proc print data = &dsn;
run;
%goto done;
%end;
%put &=startdttm;
data _null_;
call sleep(&check_interval_in_sec,1);
stop;
run;
%end;
%abort;

 data _null_;
 file print;
 put #3 @10 "Data set &dsn. does not exist";
run;

%done: ;

%mend checkds;

%checkds(sashelp.class);
%checkds(sashelp.class_not_exist);

3 REPLIES 3
Reeza
Super User
Try removing the `file print` statement from your data _null_ step.
SRINIVAS_N
Calcite | Level 5

if i remove `file print` statement from my data _null_ step nothing will happen if the dataset is not found it has to search every 15mins of 3hours ,if the dataset is not found after 3hours then it has to stop executing the program

Reeza
Super User

My understanding is that `file print;` directs the output to a print file not the log.
Removing that statement means the output would go to the log, not the file. But perhaps I don't understand your issue.

 

EDIT:

 

You need to also remove your %abort statement, it ends your process so you never reach the data _null_ step. 

This mostly works, I think your timing is off, but you can play around to adjust that to your needs.

 

%macro checkds(dsn, time_to_check_in_sec=30, check_interval_in_sec=10);
   %*set start time to track looping time;
    %local startdttm;
    %let startdttm=%sysfunc(datetime());

    %*loop control start;
    %do %while(%sysfunc(sum(%sysfunc(datetime()), -&startdttm))<&time_to_check_in_sec);

       *check if data set exists;
        %if %sysfunc(exist(&dsn)) %then
            %do;

                proc print data=&dsn;
                run;

                %goto done;
            %end;
        %put &=startdttm;

       %* not sure this works the way you think it does;
        data _null_;
            call sleep(&check_interval_in_sec, 1);
            stop;
        run;

    %end;

   *outputs the error message if data set does not exist;
    data _null_;
        put "ERROR: Data set &dsn. does not exist";
    run;
    
%*exit for loop when data is found. Could also just be logic from %else condition;
%done:

    ;
%mend checkds;

*check macro execution;
%checkds(sashelp.class);
%checkds(sashelp.class_not_exist);

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 676 views
  • 0 likes
  • 2 in conversation