BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sejin
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@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

2 REPLIES 2
Patrick
Opal | Level 21

@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);

 

 

gamotte
Rhodochrosite | Level 12

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.

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
  • 2 replies
  • 3888 views
  • 2 likes
  • 3 in conversation