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

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!!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

 

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

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;

 

xizidememeda
Calcite | Level 5

I rewrite my complete codes based on your reply and comments and it works very well!

Thanks very much for your help!!Smiley LOLHeart

gamotte
Rhodochrosite | Level 12
Hello, macro variables inside single quoted strings are not resolved.
Try
infile "D:files\price_&ym..txt" dsd dlm='09'x truncover;
instead.
xizidememeda
Calcite | Level 5

Yes, you are right! Many many thanks for your help!Smiley Wink

Reeza
Super User
Since all your files are the same you don't need to use a macro, you can use a wildcard and read them all in one step.



sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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