DATA Step, Macro, Functions and more

SAS Macro %Do %To with 2 variables

Reply
Frequent Contributor
Posts: 75

SAS Macro %Do %To with 2 variables

Using SAS EG 7.1.

 

Basically I want to merge

FATax&YEAR1.&MONTH with Y&YEAR2.&MONTH, but YEAR1 is NOT equal YEAR2.

 

For eg,

 

FATax2015.&MONTH with Y&2014.&MONTH

 

I tried the below, but it doesn't seem to work.

 

 

CODE:

 

%DO YEAR1=&FIRSTYR_Tax %TO &LASTYR_TAX;

%DO YEAR2=&FIRSTYR_Y %TO &LASTYR_Y;

%do i = 1 %to %sysfunc(countw(&monlist.));

%let month = %sysfunc(putn(%scan(&monlist.,&i.),z2.));

data Merged&YEAR1.&MONTH;

merge

FATax&YEAR1.&MONTH(IN=A) Y&YEAR2.&MONTH (IN=B);

BY UIN;

IF A AND B Or (A AND Age=0);

run;

 

%END;

%END;

%END;

 

Super User
Super User
Posts: 7,430

Re: SAS Macro %Do %To with 2 variables

[ Edited ]

Why are you creating a rod for your own back in the first place?  I would first set your data together, get rid of the data elements in the dataset name - remember data set name, and variable names, are there for programming purposes Not to contain "data".  Labels and observations are there to contain "data".  If you put your data together, and you haven't posted any example data so I can't provide code, you can then simplfy your whole code to focus on one dataset, no need for looping, or macro or anything else, simple Base SAS programming.

 

If you can post some example test data - in the form of a datastep, I will show you how to process it into well strcutured data, and then how to program using Base SAS a very simple setup.

 

Edit: I also add the link to your other question, which is pretty much the same thing and caused by the same structural choices:

http://communities.sas.com/t5/Base-SAS-Programming/Macro-with-Merge-IN-A/m-p/258419/highlight/false#...

Frequent Contributor
Posts: 75

Re: SAS Macro %Do %To with 2 variables

HI,
I am no interested in "SET" as I want to merge by UIN.

I have many datasets, so using a macro will more efficient.
THank you
Super User
Super User
Posts: 7,430

Re: SAS Macro %Do %To with 2 variables

The merge is irrelevant to the strcuture of the data, it is exactly the same to merge one dataset with itself as many to many.

 

With regards to your second point, macro will not be more efficient.  This is a fundamental lack of understanding of what macro is.  Macro is a text generation tool, which generates Base SAS code.  It and of itself does nothing.  So the question is, is the code generated by the macro processor more efficient and than the datastep I provided.  Now I haven't tested it, but as mine is one datastep, with one datastep, and therefore would incur only one read/write per operation, whereas the macro code would generate one step with one read/write per operation I would conclude that no, the macro method would be slower, not to mention the code itself is far more complicated to maintain.

 

The simple fact is that you have many datasets, which you are reading, and them merging together.  A simpler method, is to put them all together in one step and then transpose the resulting dataset.  This would have exactly the same output as your method, with two datasteps.

 

If there was some test data, I could proide code.

Super User
Posts: 6,971

Re: SAS Macro %Do %To with 2 variables

With this construct, the macro will create datasteps where

number of datasteps = cardinality of YEAR1 * cardinality of YEAR2 * cardinality of &monlist

which is a whole lot of datasteps

 

I guess you have some rule for the relationship of YEAR2 to YEAR1?

eg if the difference is 1, then

%DO YEAR1=&FIRSTYR_Tax %TO &LASTYR_TAX;
%let year2 = %eval(&year1-1);

should suffice

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 4 replies
  • 288 views
  • 0 likes
  • 3 in conversation