Use macro to append tables

Accepted Solution Solved
Reply
Regular Contributor
Posts: 212
Accepted Solution

Use macro to append tables

Hi guys,

I'm creating a query to append Datasets using a macro, because, sometimes i need to append data only from 3 months ago.

This code below works well until i create a data set inside of the macro to append the tables:

%GLOBAL YEARDT

  ANO

  MES

  INTERVALO

  ANOI

  MESI

  BASES;

%LET YEARDT = %SYSFUNC(DATE(),YYMMP10.);

%LET ANO = %SUBSTR(&YEARDT.,1,4);

%LET MES = %SUBSTR(&YEARDT.,6,2);

%PUT &ANO&MES.;

%MACRO APPEND(NUM);

%DO I = 1 %TO #

%LET INTERVALO = %SYSFUNC(PUTN(%SYSFUNC(INTNX(MONTH,%SYSFUNC(DATE()),-&&I.)),YYMMP10.));

%LET ANOI = %SUBSTR(&INTERVALO.,1,4);

%LET MESI = %SUBSTR(&INTERVALO.,6,2);

%LET BASES  = BASE_WO.BASE_WO_&ANOI&MESI.;

%PUT &BASES.;

%END;

%MEND;

%APPEND(2);

In the %APPEND(2); i'd like to append how much months i need(Type a number).

My tables names has dates at the end of their names in this format = 201501;

How can i do that to append tables ?


Thank you before anything,

Rodrigo Dartibali Elias


Accepted Solutions
Solution
‎08-19-2015 04:24 PM
Super User
Posts: 11,343

Re: Use macro to append tables

Posted in reply to DartibaliRodrigo

Show the code you used that didn't work.

A likely cause is either a missing semi colon or the location of the loop:

%macro dummyset( loops=5);

data want;

     set

     %do I = 1 to &loops;

          libref.myset&i      /* NOTE: No ; here*/

     %end; /* the LOOP but NOT the set statement*/

     ;  /* This ; ends the SET statement*/

run;

%mend;

So your code should make sure you are NOT generating any extra ;

Possibly:

data want;

     set

     %yourmacroname ()

     ;

run;

Use OPTIONS MPRINT to see the resolved code and look for an extra ;

View solution in original post


All Replies
Regular Contributor
Posts: 212

Re: Use macro to append tables

Posted in reply to DartibaliRodrigo

One more thing. When i use the macro Bases in %put statement inside do loop, it prints all tables names into the log(Works),

When i put the same macro in the set statement it doesn't work.

Super User
Posts: 19,822

Re: Use macro to append tables

Posted in reply to DartibaliRodrigo

I think querying the Dictionary Tables is an easier method.

Take a look at the methods in this thread and see if you can expand them to your problem.

Solution
‎08-19-2015 04:24 PM
Super User
Posts: 11,343

Re: Use macro to append tables

Posted in reply to DartibaliRodrigo

Show the code you used that didn't work.

A likely cause is either a missing semi colon or the location of the loop:

%macro dummyset( loops=5);

data want;

     set

     %do I = 1 to &loops;

          libref.myset&i      /* NOTE: No ; here*/

     %end; /* the LOOP but NOT the set statement*/

     ;  /* This ; ends the SET statement*/

run;

%mend;

So your code should make sure you are NOT generating any extra ;

Possibly:

data want;

     set

     %yourmacroname ()

     ;

run;

Use OPTIONS MPRINT to see the resolved code and look for an extra ;

Regular Contributor
Posts: 212

Re: Use macro to append tables

The code i used works but only prints the last table printed in the log.

Regular Contributor
Posts: 212

Re: Use macro to append tables

Posted in reply to DartibaliRodrigo

Thanks Ballard it works

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 253 views
  • 2 likes
  • 3 in conversation