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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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