DATA Step, Macro, Functions and more

Help with &&var&i?

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Help with &&var&i?

I've spent hours on this, but it will not work when I try to run it as a dataset. What I am trying to do is read in multiple datasets with the same ending, but different prefixes. Such as:

d170726_1 d170726_2 d170822 d170925

They are datasets within the same fiscal quarter, so I want to combine them into one quarter overall, but first I must run some logic. It is much more simpler to do this with a macro since this will be a continuing issue for the entire year since the data comes in waves. I want to make the process generalizable, but there needs to be some sorting, etc. before the merging. So... I placed these names in a .txt file using:

data null; length dsn $9. ; infile "&proj.&sp.CY2017&sp.datasets.txt" truncover; input @1 dsn $9. ;

i+1;
ii = left(put(i,2.));
call symput('n',ii);
call symput('dsn'||ii,left(dsn));

run;

when run %put all , the codes show: dsn1 = 1232_test, dsn2 = 42521_1_test, etc.

Cool great. I use this in the code data&year..&&dsn&i._mst_tbl... And these are my results D:

d170726_1 WARNING: Apparent symbolic reference DSN1_MST_TBL not resolved. WARNING: Apparent symbolic reference DSN1_CPT_CODE not resolved. WARNING: data2017.&dsn1_cpt_code does not exist

Please help Smiley Sad What am I doing wrong? I'm going insane.


Accepted Solutions
Solution
a month ago
New Contributor
Posts: 3

Re: Help with &&var&i?

Posted in reply to Astounding

My apologies. Yes, the dataset should resolve to the dsn1, dsn2, and so forth variables which should contain the actual dataset name so that dsn1 the 1st dataset listed in the .txt file.

View solution in original post


All Replies
SAS Super FREQ
Posts: 8,869

Re: Help with &&var&i?

Posted in reply to friedtukeywings
Hi:
You do not show enough code. For example, you do not show how &proj, &sp, &I or &year is created. You only show CALL SYMPUT for &dsn.

The warning in the LOG is trying to tell you that something about your indirect reference is specified incorrectly. An indirect reference is when you use multiple ampersands to delay the resolution for part of a macro variable reference while you another part of the macro variable gets resolved. Then due to the way that macro variables are scanned and resolved, multiple ampersands cause the Macro processor to "hold" and "rescan" a reference with multiple && until all the resolution and substitution are finished.

For a simple example, please refer to this paper: https://support.sas.com/resources/papers/proceedings13/120-2013.pdf on page 10-11 at the discussion of indirect reference in regard to the macro variable reference &&muppet&num.

cynthia
New Contributor
Posts: 3

Re: Help with &&var&i?

[ Edited ]
Posted in reply to Cynthia_sas

I am sorry,

 

This code is set within a large macro such as:

 

%macro sashelp (year =, ms_tbl =);

 

Which then corresponds to:

%sashelp (2015, garden) and so forth.

 

[Edit] Forgot to answer this, the &proj and &_sp_ macro variables are just the path of the file.

Super User
Posts: 5,518

Re: Help with &&var&i?

Posted in reply to friedtukeywings

I'm not sure you showed us what you want this to resolve into:

 

&&dsn&i._mst_tbl

 

So I'll assume it should take the value &DSN1 (then &DSN2, then &DSN3, etc.), and append _mst_tbl at the end.  If that's the intent, a small change should take care of things

&&dsn&i.._mst_tbl

 

With two dots in the middle, the first one delimits &i, then the second one delimits &DSN1.

Solution
a month ago
New Contributor
Posts: 3

Re: Help with &&var&i?

Posted in reply to Astounding

My apologies. Yes, the dataset should resolve to the dsn1, dsn2, and so forth variables which should contain the actual dataset name so that dsn1 the 1st dataset listed in the .txt file.

Super User
Super User
Posts: 7,083

Re: Help with &&var&i?

Posted in reply to friedtukeywings

What am I doing wrong?

Perhaps it is that you are making your program too complicated?

 

Someone already has given you the answer that you need to add another period to mark the end of the &DSNx macro reference since you need to resolve two different macro variables in front of the fixed text. 

&&dsn&i.._MST_TBL 

That should have been pretty clear from the warning messages

WARNING: Apparent symbolic reference DSN1_MST_TBL not resolved.
WARNING: Apparent symbolic reference DSN1_CPT_CODE not resolved.

that you got.

 

But if you just expand your code to make the steps a little clearer it will be easier for both you and SAS to figure out what you are doing.

%do i=1 to &nobs ;
....
%let basename=&&dsn&i ;
%let master=&basename._MST_TBL ;
%let cptfile=&basename._CPT_CODE;
data &master ;
  set &cptfile ;
  ....

%end;
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 92 views
  • 0 likes
  • 4 in conversation