BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ronein
Meteorite | Level 14

This is working perfect!

%macro RUNIMPORTS; 
%do j=1 %to &count.;
%let YYMMDD=%scan(&vector.,&j.,+);
%import(&YYMMDD.);
%end;
%mend RUNIMPORTS;
%RUNIMPORTS;

View solution in original post

4 REPLIES 4
ballardw
Super User

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.

Ronein
Meteorite | Level 14
%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?

ballardw
Super User

@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.

 

 

 

 

 

Ronein
Meteorite | Level 14

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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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