BookmarkSubscribeRSS Feed
ANIRBAN2
Fluorite | Level 6

%Macro sleep(SourcePath=);
%global rc name did mem_cnt did csv_count dnum;
%let csv_count=0;
%let ext=%str(csv);
%let rc=%sysfunc(filename(filrf, &SourcePath));

%if &rc=0 %then
%put SUCCESS;
%else
%do;
%put NO SUCCESS;
%end;
%let did=%sysfunc(dopen(&filrf));

%if &did=0 %then
%put CANT OPEN;
%else
%do;
%put OPENED;
%end;

%if &did.> 0 %then
%let mem_cnt= %sysfunc(dnum(&did.));

%if &mem_cnt. =0 %then
%do;
%let x=%sysfunc(SLEEP(10, 1)));
%sleep(SourcePath=&sourcepath);

/*%put "NO FILES IN THE FOLDER";*/
%end;
%else
%do;

/*-------------*/
%PUT THERE ARE &mem_cnt. files in the DIR;

%do i=1 %to &mem_cnt.;

/*-------------*/
%let full_name=%qsysfunc(dread(&did., &i.));
%let name=%qscan(%qsysfunc(dread(&did, &i)), -1, .);

%if %str(csv)=%str(&name.) %then
%do;

/*-------------*/
%put CSV PRESENT;

proc import datafile="&sourcepath/&full_name" out=work.&i.
dbms=csv replace;
Run;

%end;
%end;
%end;
%mend;

 

%sleep(SourcePath=/home/u60099960/ani);
%put &mem_cnt;
%put &name;
%put &rc;

 

LOGS: After every 10sec

 
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
68
69 %sleep(SourcePath=/home/u60099960/ani);
SUCCESS
OPENED
NO SUCCESS
OPENED
NO SUCCESS
OPENED
NO SUCCESS
OPENED
NO SUCCESS
OPENED

My Question when I am calling the %sleep(SourcePath=&sourcepath) after execution of sleep function then from the 2nd time rc is not equal 0 so its showing  NO success.

My question is why from the 2nd time is showing no success.?

 

7 REPLIES 7
andreas_lds
Jade | Level 19

Sorry, but your code is hardly readable. Please use "insert sas code" (running man icon) and past properly formatted code.

ANIRBAN2
Fluorite | Level 6
I have paste the code with the logs I hope it is readable now.
My problem is when code is calling the macro again and again from 2nd time Rc is comming NOT success.
THIS CODE WILL SEARCH A FOLDER IF ANY FILE IS PRESENT IN A FOLDER OR NOT, IFNOT THEN RUN THE PROGRAM AFTER 10SEC GAIN AND AGAIN.
IF ANY FILE FOUND THEN IMPORT THE CSV FILES.
ANIRBAN2
Fluorite | Level 6
My problem is when code is calling the macro again and again from 2nd time Rc is comming NOT success.
THIS CODE WILL SEARCH A FOLDER IF ANY FILE IS PRESENT IN A FOLDER OR NOT, IFNOT THEN RUN THE PROGRAM AFTER 10SEC GAIN AND AGAIN.
IF ANY FILE FOUND THEN IMPORT THE CSV FILES
Kurt_Bremser
Super User

You do not need a recursion, a simple %DO loop will do it:

%macro sleep(sourcepath=);
%local rc did count numfiles name; /* local to avoid side effects */
filename ___dir "&sourcepath.";
%let did = %sysfunc(dopen(___dir));
%if &did. = 0
%then %put Error opening directory;
%else %do;
  %let count = 0;
  %let numfiles = %sysfunc(dnum(&did.));
  %do %while (&count. le 3 and &numfiles. = 0);
    %let count = %eval(&count. + 1);
    %let numfiles = %sysfunc(dnum(&did.));
    %let rc = %sysfunc(sleep(10,1));
  %end;
  %if &numfile. gt 0
  %then %do i = 1 %to &numfiles.;
    %let name = %sysfunc(dread(&did.,&i.));
    proc import
      datafile="&sourcepath./&name."
      out=work.ds&i.
      dbms=csv
      replace
    ;
    run;
  %end;
  %else %put No files found;
  %let rc = %sysfunc(dclose(&did.)); /* always clean up */
%end;
%else %put Cannot open directory;
filename ___dir clear;
%mend;

I added a count to prevent infinite looping if the directory is never populated.

Checking the return code of a FILENAME function is not necessary, as the function will always work (even if a file/directory does not exist, the fileref could be used to create it), so I used FILENAME statements.

Cleaning up file/directory handles is necessary as they will accumulate otherwise.

ANIRBAN2
Fluorite | Level 6
THIS I COULD UNDERSTAND
THANKS.
BUT PLEASE CAN U IDENTIFY THE ERROR IN MY CODE
Kurt_Bremser
Super User

I guess the culprit is your %GLOBAL statement, and it is not only not necessary, but dangerous. Use %LOCAL instead.

Why? A variable defined as local to a macro will appear as global for all macros that are called from this macro anyway. By using %GLOBAL, you force the called macro to use the variable it inherited through the call, and it then manipulates a value that should have been kept as-is for the "outer" macro.

All good macros take care to define all variables as local, unless an explicit side-effect is wanted, or the variables' contents are never changed in the macro.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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