Hello
I want to import files into SAS.
I need to use conditional import because it might happen that some files are not existing.
The files that I import has name Offers with end of YYMMDD (for example :Offers190815)
I run it but there is no output.
In log i see message "NOTE: CALL EXECUTE routine executed successfully, but no SAS statements were generated."
What is wrong with mmacro1?
%macro import(YYMMDD=);
%if %sysfunc(fileexist(/path/Offers&YYMMDD. ))
%then %do;
data Offers&YYMMDD.;
infile "/path/Offers&YYMMDD." LRECL=54 recfm=f;
input
ID s370fzd8.
OFFER s370fzd8.;
run;
%end;
%mend import;
%let vector='15AUG2019'd+'20AUG2019'd+'25AUG2019'd;
%let count = %sysfunc(countw(&vector));
%put &vector;
%put &count;
%macro mmacro1;
%do j=1 %to &count.;
%let YYMMDD=%scan(&vector.,&j.,+);
data _null_;
date=put(YYMMDD,yymmdd6.); /*char in form YYMMDD*/
call execute(cats('%import(YYMMDD=',date,')'));
run;
%end;
%mend mmacro1;
%mmacro1;
This is working perfect!
%macro RUNIMPORTS;
%do j=1 %to &count.;
%let YYMMDD=%scan(&vector.,&j.,+);
%import(&YYMMDD.);
%end;
%mend RUNIMPORTS;
%RUNIMPORTS;
Did you run the code with OPTIONS MPRINT; to see the code being generated?
Macros like your MMacro1 that assume a variable is available like your &vector &count that are not passed as a parameter are always a tad problematic in general. Better would be to pass the value of Vector as a parameter in the macro and then calculate Count inside the macro.
Since your code for reading the file implies there is no header row you might consider reading multiple files with one data step, possibly with a wildcard in the INFILE statement and if you need to know the source file use the FILENAME option to capture that.
%let vector='15AUG2019'd+'20AUG2019'd+'25AUG2019'd;
%let count = %sysfunc(countw(&vector));
%put &vector;
%put &count;
%macro mmacro1;
%do j=1 %to &count.;
%let YYMMDD=%scan(&vector.,&j.,+);
%import(&YYMMDD);
%end;
%mend mmacro1;
%mmacro1;
I am not near sas now. Do you think this code will work well?
@Ronein wrote:%let vector='15AUG2019'd+'20AUG2019'd+'25AUG2019'd; %let count = %sysfunc(countw(&vector)); %put &vector; %put &count; %macro mmacro1; %do j=1 %to &count.; %let YYMMDD=%scan(&vector.,&j.,+);
%import(&YYMMDD);
%end;
%mend mmacro1;
%mmacro1;
I am not near sas now. Do you think this code will work well?
I will bet a largish stack of $$ that it won't.
As I asked, did you look at the code generated using option mprint and in this case MLOGIC is helpful?
Partial output:
82 options mprint mlogic; 83 %mmacro1; MLOGIC(MMACRO1): Beginning execution. MLOGIC(MMACRO1): %DO loop beginning; index variable J; start value is 1; stop value is 3; by value is 1. MLOGIC(MMACRO1): %LET (variable name is YYMMDD) MPRINT(MMACRO1): data _null_; MPRINT(MMACRO1): date=put(YYMMDD,yymmdd6.); MPRINT(MMACRO1): call execute(cats('%import(YYMMDD=',date,')')); MPRINT(MMACRO1): run; NOTE: Variable YYMMDD is uninitialized. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
Your
date=put(YYMMDD,yymmdd6.);
is using a data step variable, YYMMDD which has no source, not the macro variable &YYMMDD.
This is working perfect!
%macro RUNIMPORTS;
%do j=1 %to &count.;
%let YYMMDD=%scan(&vector.,&j.,+);
%import(&YYMMDD.);
%end;
%mend RUNIMPORTS;
%RUNIMPORTS;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.