Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sas macro variable not resolving

Status
Not open for further replies.

mdieckman

MIS
Nov 21, 2006
88
US
I am trying to get a macro variable to resolve using a counter &CNT. So, the first variable would be cov1, cov2,etc. The statement runs fine, but when I run the %PUT _ALL_; statement nothing is returned. I know it is reading records from the data set because it works if I use a manual name. Am I doing something wrong in the loop or the SYMPUT statement that wouldn't cause an error?

Here is the code:

%MACRO cov07;
%DO CNT = 1 %TO &M %BY 1;
%PUT ;
%PUT ;
%PUT CNT = &CNT;
%PUT ;
%PUT ;

DATA _NULL_;
SET COV07;
CALL SYMPUT("COV&CNT.",COVERAGE&CNT);
RUN;

%END;

%MEND cov07;

Any suggestions?
 
Hi,
What are you ultimately trying to achieve here? If you want to list the values you've got in your macro variables, I'm not sure that _all_ will work. You can use the dictionary tables though to get this info.
Here's an example:-
Code:
%let cov1=Covenent1;
%let cov2=Covenant2;

proc sql;
  create table macros as
  select *
  from dictionary.macros
  where name like 'COV%'
  ;
quit;

proc print data=macros noobs label;
  var name value;
run;


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Hi Chris,

I am trying to use the SYMPUT statement to iteratively put a list of values into a list of macro variables so I can refer to them later in the program where I wouldn't have the ability to output them without a macro variable.

I am pretty sure the _all_ feature works.

If I make the symput statement look like this:

CALL SYMPUT('COV1',COVERAGE1);

And then do the _all_ statement I can see the value of the macro variable. Something within the count variable (&CNT) is not allowing the macro variable to resolve. I'm sure of this because later in the program I refer to &cov1 and it doesn't know what it is. I'm at a loss though why this wouldn't work and it's even more bizarre because it did work at one point, but hasn't since and I didn't change anything...

I was messing around with it later this afternoon before I posted this and diregard the . after &CNT in the symput statement. I forgot to take that out before posting it...
 
Ah!
I think I have it.
Macro variables created in a macro are not by default available outside the macro. The macro variables are in fact declared as LOCAL, but you need them to be GLOBAL. I'm just going to play around with the code for a minute, but I think I can get this workign for you...


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
OK, unfortunately I couldn't get what I wanted working. I was hoping that you could change the scope of the variable after it had been defined, but you can't. If you use my proc sql step shown above, within your macro, you can see that the SCOPE of the variable will be listed as the name of your macro, ie it only works within that macro. If you run it after your macro, you'll get no records back.
What you need is to create a list of the variables, before they are created then use this:-
Code:
  %global cov1 cov2 cov3;
Unfortunately it seems as if you can't change a macro variables scope once it has been defined.

This might do it for you though, if you put it at the start of your macro...
[/code]
DATA vars;
do i=1 to &M;
var = catt('COV',left(put(i,3.)));
output;
end;
RUN;

proc sql;
select var
into :globs separated by ' '
from vars
;
quit;

%GLOBAL &GLOBS;
[/code]


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
That's what I needed Chris thanks!

I added %GLOBAL COV&CNT; inside my do loop and that did the trick. Thanks for pointing me in the right direction!
 
Awesome, yours is a much simpler method! :)

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top