DATA Step, Macro, Functions and more

Sticky Nested Macro variables

Reply
Contributor
Posts: 65

Sticky Nested Macro variables

I am running a macro loop that contains macro functions like so:

%macro loop;
%do j=1 %to 1000;
%macro variable;
%end;
%mend;

The macro variable itself has some macro functions enclosed like so:

%macro variable;

%macro rename(x);

proc sql;
create table Renamed&j&x as
select *,

rename1 as rename1,
rename2 as rename2,
...

from rename&j&x
quit;

%rename(1);
%rename(2);
....

%mend;
%mend;

What is the correct syntax for these sticky, nested macro loops and variables? I seem to remember using &&j and &x but I'm getting errors.

The code works if i replace all &j with a 2, so the code is fine, the recursive nature of the loop isn't injecting the variable correctly. TIA.

Super User
Super User
Posts: 9,227

Re: Sticky Nested Macro variables

You really are making your coding life difficult you know.  Not just this question but every single line of code you write with this is going to involve loads of macro code that is inefficient and unstable.  There should never be a need to loop over things using macro - it immediately flags either a data modelling issue, or other organisation issue.  Why for example do you want to rename loads of variables in loads of datasets?  As before, keeping data in a normalised method will reduce operations to one, keeping all like data in one dataet, will remove loops, hence data is processed once, in one place, using simple syntax.  But even then there would be a question of why you need to rename variables - either you created them, in which why are they not labelled correctly, or B you get the data from third party, and then why is the data transfer agreement not in place or correct.  

Contributor
Posts: 65

Re: Sticky Nested Macro variables

sas is generally making my life a bit more hard work because I can't do cross-row matrix multiplication. So typically I have to do row by row work then place this all up into a table and each output i'll need to keep the work saved as a subset to work with it in another area or work with it later on as all these tables are required later for other matrix manipulation

Contributor
Posts: 65

Re: Sticky Nested Macro variables

I've taken out this section and I'm working back through it to tidy it up but I still need iterations of every segment to perform its own matrix multiplication with previous matrices.

Super User
Super User
Posts: 9,227

Re: Sticky Nested Macro variables

If your doing matrix calculations, then proc iml is likely the way to go.

This "still need iterations of every segment" - iterations are done by using br groups in SAS.  So for instance, to do a means based on a variable called segment, you could do:

proc means data=have;
  by segment;
...
run;

By group processing is very powerful and a lot faster than multiple tables and lots of loops - you can have any number of groups in a dataset, just setup a variable to assign your groups, then by those groups.

Contributor
Posts: 65

Re: Sticky Nested Macro variables

Unfortunately, I've been told I can't use that language package for SAS.

 

Do you have any articles / books / literature regarding working with groups for big data. I'm going to tidy up my project code after doing some research.

 

thanks

Valued Guide
Posts: 561

Re: Sticky Nested Macro variables

I'm with @RW9 on. this one - in 30 years as a professional software developer I've never felt the need to write a recursive macro. The job itself is hard enough without all those complications....

Contributor
Posts: 65

Re: Sticky Nested Macro variables

Posted in reply to ChrisBrooks

I've got a deadline for Friday. I hope that i'll have enough time to tidy up my code using groups but as it stands with my inexperience with SAS i'd like to just finish it simply first, then improve it. I'll definitely be practicing with groups in my spare time for the next project.

Super User
Posts: 9,611

Re: Sticky Nested Macro variables


89974114 wrote:

I've got a deadline for Friday. I hope that i'll have enough time to tidy up my code using groups but as it stands with my inexperience with SAS i'd like to just finish it simply first, then improve it. I'll definitely be practicing with groups in my spare time for the next project.


"Simply" is not a word I'd use in conjunction with SAS macro programming. Especially for novices, it complicates more than it solves.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 65

Re: Sticky Nested Macro variables

Posted in reply to KurtBremser

I've managed to finish my project. Do you have any books or articles that will help me tidy up my sas code? In focus with big data analysis by grouping. I would appreciate the guidance!

Contributor
Posts: 65

Re: Sticky Nested Macro variables

Posted in reply to ChrisBrooks

i'm assuming I'd have to do all my work transposed if my data looks like:

 

ID    17    18    19    20

17    1     2       5      3
18
19
20

and my work needs to be done by ID_year e.g if two tables are identical but one is for basis points and the other maturity. I need to multiply

Row 1 x row1 of both tables
row 2 of the new table by row2 of another table

and other complicated work with matrices for typical sas code.
Super User
Super User
Posts: 7,860

Re: Sticky Nested Macro variables


89974114 wrote:

i'm assuming I'd have to do all my work transposed if my data looks like:

 

ID    17    18    19    20

17    1     2       5      3
18
19
20

and my work needs to be done by ID_year e.g if two tables are identical but one is for basis points and the other maturity. I need to multiply

Row 1 x row1 of both tables
row 2 of the new table by row2 of another table

and other complicated work with matrices for typical sas code.

A matrix is just a way of storing a relation. So instead of storing it as a matrix store it as a relational table.

Row Column Value
17 17  1
17 18  2
17 19  5
17 20  3
18 17 6
18 18 7
...

Of course you will probably want to use more meaningful variable names.

Super User
Posts: 9,611

Re: Sticky Nested Macro variables

NESTED MACRO DEFINITIONS MAKE NO SENSE.

 

Why?

All defined macros reside in the global macro symbol table, there is no locality AT ALL.

Only macro variables can exist in local or global symbol tables.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 12 replies
  • 125 views
  • 0 likes
  • 5 in conversation