BookmarkSubscribeRSS Feed
gsaba
Calcite | Level 5

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? 

4 REPLIES 4
PaigeMiller
Diamond | Level 26

@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
gsaba
Calcite | Level 5

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 😞

ballardw
Super User
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.

PaigeMiller
Diamond | Level 26

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


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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1023 views
  • 2 likes
  • 3 in conversation