BookmarkSubscribeRSS Feed
smilingmelbourne
Fluorite | Level 6
Hi everyone, I can't figure out why this code doesn't work. Specificially, the &varnum doesn't get resolved.

options symbolgen;
options mprint;
options mlogic;
%macro count_vars(dataset);
%local varnum;
proc sql;
select count(*) into : varnum
from dictionary.columns
where memname=upcase("&dataset");
quit;
%mend count_vars;

%count_vars(Mf_hist_holdings_all)
%put varnum = &varnum;

The ERROR message is like below:

"MLOGIC(COUNT_VARS): Ending execution.
WARNING: Apparent symbolic reference VARNUM not resolved.
421 %put varnum = &varnum;
varnum = &varnum"


By the way, I don't understand why this code doesn't run.

%macro count_vars(dataset, varnum);
proc sql;
select count(*) into : varnum
from dictionary.columns
where memname=upcase("&dataset");
quit;
%mend count_vars;

%count_vars(Mf_hist_holdings_all, varnum)


The reason is I don't want to remember that varnum is a variable that contains the number of variables in a dataset. Imagine I have 100 macros and everytime I run one such macro, I have to open the source code and check what name that variable has, i.e. here varnum. So when I clean a dataset, I can do the following

%global myvars;
%count_vars(mydataset, myvars)


INSTEAD OF openning the source code and checking that the name is varnum and only then can I put "varnum" as an argument to the macro %count_vars.

Can somebody please advise?

Message was edited by: smilingmelbourne Message was edited by: smilingmelbourne
4 REPLIES 4
art297
Opal | Level 21
Your first question is easy: you defined varnum as local!

If you add a %put statement within the macro you will see that it does, in fact, get resolved. E.g.,
[pre]

%macro count_vars(dataset);
%local varnum;
proc sql;
select count(*) into : varnum
from dictionary.columns
where memname=upcase("&dataset")
;
quit;
%put varnum = &varnum;
%mend count_vars;

%count_vars(class)
%put varnum = &varnum;
[/pre]
HTH,
Art
Patrick
Opal | Level 21
Hi
You probabely should amend your where clause in order to avoid issues for cases where you have a table with the same name in more than one library.

where libname=%upcase("%scan(work.&dataset,-2)") and memname=%upcase("%scan(&dataset,-1)")

HTH
Patrick P.S: What's the story of your name?
http://www.kitezh.com/texts/melbsmile.htm :-))


Message was edited by: Patrick
Ksharp
Super User
If you want the number of variables in a dataset.then use dictionary.tables, in it there is column named nvar is what you need.


Ksharp
smilingmelbourne
Fluorite | Level 6
Thank you all very much for the help. I've learnt a lot.

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
  • 4 replies
  • 7650 views
  • 0 likes
  • 4 in conversation