DATA Step, Macro, Functions and more

Macros - Loop

Reply
Occasional Contributor
Posts: 9

Macros - Loop

Hi Guys, 

 

I have a small issue about SAS Macro: 

 

i have 5 tables: 

 

trans2018_01, trans2018_02, trans2018_03, trans2018_04, trans2018_05

 

i need to append it with some kind of criteria. if i'll write 1,2,3 program has to be able to append first three tables. when 1,2,3,4 - 4 tables. they are titled by months. 

 

i have a small construction of code: 

 

input -> startyear, startmonth, endyear, endmonth

 

%let startdate,

%let enddate

 

nummonths -> (for understanding how many months are in these period)

 

proc sql; 

 

append; 

 

can anyone tell me how my code should really be? 

Respected Advisor
Posts: 3,251

Re: Macros - Loop

[ Edited ]

@gsaba wrote:

 

 

i need to append it with some kind of criteria. if i'll write 1,2,3 program has to be able to append first three tables. when 1,2,3,4 - 4 tables. they are titled by months. 

 


This is rather vague.

 

But ... the advice is to get a version of the code working WITHOUT macros first. Then, turning it into a macro is less of a problem, and if you show us the code working WITHOUT macros, we can probably give you advice.

--
Paige Miller
Occasional Contributor
Posts: 9

Re: Macros - Loop

Posted in reply to PaigeMiller

now i wrote this: 

 

%macro appending (startmonth, endmonth);

data file1;
input(gsaba.products_2018_01, date9.);
run;

data file2;
input(gsaba.products_2018_02, date9.);
run;

data file3;
input(gsaba.products_2018_03, date9.);
run;

data file4;
input(gsaba.products_2018_04, date9.);
run;

data file5;
input(gsaba.products_2018_05, date9.);
run;

data appendedtables;
set

%do 'i'=&startmonth %to &endmonth;
file&'i'
%end;
run;

%mend;

%appending (startmonth = 1, endmonth = 5);

 

but it doesn't work Smiley Sad

Super User
Posts: 13,889

Re: Macros - Loop

data file1;
input(gsaba.products_2018_01, date9.);
run;

Will generate all kinds of errors because input either expects to read from an external file identified in an infile statement or data supplied in line with the data step code following a CARDS or DATALINES statement. Input also only uses () to indicate groups of variables to be treated with the same reading instructions.

 

Which means you did not have working code before attempting the macro.

 

If you already have a sas data set gsaba.products_2018_01 the code might look like:

data file1;

   set gsaba.products_2018_01;

run;

 

but likely not needed for your stated purpose.

 

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

 

consider:

%macro appending (startmonth=, endmonth=);
data appendedtables;
  set gsaba.products_2018_&startmonth. - gsaba.products_2018_&endmonth.;
run;
%mend;

%appending (startmonth = 01, endmonth = 05);

Notice the use of TWO digits for the start and endmonth to allow simple substitution involving the actual suffix.

 

So  assuming that you never have to deal with multiple years you could select May to November with startmonth=05 and endmonth=11 and never have to create file1, file2 … file12.

Respected Advisor
Posts: 3,251

Re: Macros - Loop

[ Edited ]

@gsaba wrote:

now i wrote this: 

 

%macro appending (startmonth, endmonth);

data file1;
input(gsaba.products_2018_01, date9.);
run;

data file2;
input(gsaba.products_2018_02, date9.);
run;

data file3;
input(gsaba.products_2018_03, date9.);
run;

data file4;
input(gsaba.products_2018_04, date9.);
run;

data file5;
input(gsaba.products_2018_05, date9.);
run;

data appendedtables;
set

%do 'i'=&startmonth %to &endmonth;
file&'i'
%end;
run;

%mend;

%appending (startmonth = 1, endmonth = 5);

 

but it doesn't work Smiley Sad


Well, the advice was to get this to work in one situation, one iteration of the loop, without macros. The code you show above still uses macros, which I have highlighted in red. So you are not yet at the situation that we are asking you to achieve, that is WORKING code WITHOUT macros.

--
Paige Miller
Ask a Question
Discussion stats
  • 4 replies
  • 88 views
  • 2 likes
  • 3 in conversation