BookmarkSubscribeRSS Feed
89974114
Quartz | Level 8

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.

12 REPLIES 12
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

89974114
Quartz | Level 8

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

89974114
Quartz | Level 8

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

89974114
Quartz | Level 8

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

ChrisBrooks
Ammonite | Level 13

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

89974114
Quartz | Level 8

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.

Kurt_Bremser
Super User

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

89974114
Quartz | Level 8

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!

89974114
Quartz | Level 8

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

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

Kurt_Bremser
Super User

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.

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
  • 12 replies
  • 1360 views
  • 0 likes
  • 5 in conversation