DATA Step, Macro, Functions and more

Using two macro variables with multiple & in sas macro

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Using two macro variables with multiple & in sas macro

[ Edited ]

Hello.

I am having difficulty in quoting macro variables in my macro code.

Here is a brief example of my problem.

 

data _null;
set data1;
call symputx('fund'||left(_n_),crsp_portno);
run;

 

data _null;
set data2;
call symputx('time'||left(_n_),date);
run;

 

%macro name;

%local i;
%local j;

%do i=1 %to 2;
%do j=1 %to 2;

proc sql;
create table fund. f&&fund&i&&time&j._raw as
select * from somewhere
where crsp_portno=&&fund&i
quit;

%end;

%end;

%mend;

 

Espeically, I am having trouble with using the expression

 

f&&fund&i&&time&j._raw

 

 

I was expecting this code plays like

 

f&&fund&i&&time&j._raw

->

f&fund1&time1._raw

->

f100000120120331_raw

 

this way.

(Those values that were assigned to &fundi and &timej are all numeric with 7 or 8 digits)

 

 

However, it seems that I have poor understanding in using multiple &.

Could anyone give an idea for this?

Thanks for reading my thread.


Accepted Solutions
Solution
‎03-01-2018 03:32 AM
Respected Advisor
Posts: 4,791

Re: Using two macro variables with multiple & in sas macro

[ Edited ]

@Sejin It's highly likely not a good design decision to create that many SAS tables. Not sure what you're trying to achieve but an index or something in this direction would eventually be a better approach.

 

But to answer your question: Each iteration for resolving the macro variable will consume one dot. So for your result to end-up with what you're after, you need at least two such dots (for the two ampersand to get resolved).

f&&fund&i&&time&j.._raw

 

For the call symputx() bit code as below would be cleaner:

call symputx(cats('fund',put(_n_,f5.)),crsp_portno);

 

 

View solution in original post


All Replies
Solution
‎03-01-2018 03:32 AM
Respected Advisor
Posts: 4,791

Re: Using two macro variables with multiple & in sas macro

[ Edited ]

@Sejin It's highly likely not a good design decision to create that many SAS tables. Not sure what you're trying to achieve but an index or something in this direction would eventually be a better approach.

 

But to answer your question: Each iteration for resolving the macro variable will consume one dot. So for your result to end-up with what you're after, you need at least two such dots (for the two ampersand to get resolved).

f&&fund&i&&time&j.._raw

 

For the call symputx() bit code as below would be cleaner:

call symputx(cats('fund',put(_n_,f5.)),crsp_portno);

 

 

Super Contributor
Posts: 359

Re: Using two macro variables with multiple & in sas macro

Hello,

 

Please provide some example datasets in the form of data steps that can help us reproduce the problem

and tell us exactly he results you obtain. What is the log saying ?

 

If you have macrovariables foo_1, foo_2, ..., foo_n and a counter &i., the way to resolve them is &&foo_&i..

 

At first glance, your program seems correct except that there is a space after "fund." in your create table instruction.

 

Do you really need to create separate tables for each fund, time ? You can probably achieve your goal using

by group processing.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 142 views
  • 2 likes
  • 3 in conversation