- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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
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.?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Sorry, but your code is hardly readable. Please use "insert sas code" (running man icon) and past properly formatted code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Please describe in plain language what your macro is supposed to do.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
THANKS.
BUT PLEASE CAN U IDENTIFY THE ERROR IN MY CODE
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.