Hello guys, I write a sas macro to import multiple txt files as follows, however, it fails no matter how I improve it.
The log says the physical files don't exist.
Can anyone help me?
%macro all_on_index(syear=, eyear=);
%do year = &syear %to &eyear;
%do month = 1 %to 12;
%let ym = %eval(&year*100 + &month);
data work.op;
infile 'D:files\price_&ym..txt' dsd dlm='09'x truncover;
input var1:$20. var2:YYMMDD8.;
%if year = &syear and month = 1 %then %do;
data work._all;
set work.op;
run;
%end;
%else %do;
data work._all;
set work._all work.op;
run;
%end;
%end;
%end;
%mend all_options_on_index;I will appreciate it very much for your help!!
I marked the issues:
%macro all_on_index(syear=, eyear=);
%local year month ym; /* make your macro behave if it's called from another macro that uses the same variables */
%do year = &syear %to &eyear;
%do month = 1 %to 12;
%let ym = %eval(&year*100 + &month);
data work.op;
infile "D:files\price_&ym..txt" dsd dlm='09'x truncover; /* use double quotes to resolve macro variables */
input var1:$20. var2:YYMMDD8.;
run; /* always a good idea to define clear step boundaries */
%if &year = &syear and month = 1 %then %do; /* "year" is just a string, use &year for the macro variable */
data work._all;
set work.op;
run;
%end;
%else %do;
data work._all;
set work._all work.op;
run;
%end;
%end;
%end;
%mend all_options_on_index;
You can simplify the code:
%macro all_on_index(syear=, eyear=);
%local year month ym;
%do year = &syear %to &eyear;
%do month = 1 %to 12;
%let ym = %eval(&year*100 + &month);
data work.op;
infile "D:files\price_&ym..txt" dsd dlm='09'x truncover;
input var1:$20. var2:YYMMDD8.;
run;
data work._all;
set
%if &year ne &syear or &month ne 1 %then work._all;
work.op
;
run;
%end;
%end;
%mend all_options_on_index;
I marked the issues:
%macro all_on_index(syear=, eyear=);
%local year month ym; /* make your macro behave if it's called from another macro that uses the same variables */
%do year = &syear %to &eyear;
%do month = 1 %to 12;
%let ym = %eval(&year*100 + &month);
data work.op;
infile "D:files\price_&ym..txt" dsd dlm='09'x truncover; /* use double quotes to resolve macro variables */
input var1:$20. var2:YYMMDD8.;
run; /* always a good idea to define clear step boundaries */
%if &year = &syear and month = 1 %then %do; /* "year" is just a string, use &year for the macro variable */
data work._all;
set work.op;
run;
%end;
%else %do;
data work._all;
set work._all work.op;
run;
%end;
%end;
%end;
%mend all_options_on_index;
You can simplify the code:
%macro all_on_index(syear=, eyear=);
%local year month ym;
%do year = &syear %to &eyear;
%do month = 1 %to 12;
%let ym = %eval(&year*100 + &month);
data work.op;
infile "D:files\price_&ym..txt" dsd dlm='09'x truncover;
input var1:$20. var2:YYMMDD8.;
run;
data work._all;
set
%if &year ne &syear or &month ne 1 %then work._all;
work.op
;
run;
%end;
%end;
%mend all_options_on_index;
I rewrite my complete codes based on your reply and comments and it works very well!
Thanks very much for your help!!![]()
![]()
Yes, you are right! Many many thanks for your help!![]()
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.