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.
@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);
@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);
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.