<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: SAS macro for counting the number of variables/columns in a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-counting-the-number-of-variables-columns-in-a/m-p/60359#M13075</link>
    <description>Your first question is easy: you defined varnum as local!&lt;BR /&gt;
&lt;BR /&gt;
If you add a %put statement within the macro you will see that it does, in fact, get resolved.  E.g.,&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
%macro count_vars(dataset);&lt;BR /&gt;
%local varnum;&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  select count(*) into : varnum&lt;BR /&gt;
    from dictionary.columns&lt;BR /&gt;
      where memname=upcase("&amp;amp;dataset")&lt;BR /&gt;
  ;&lt;BR /&gt;
quit;&lt;BR /&gt;
%put varnum = &amp;amp;varnum;&lt;BR /&gt;
%mend count_vars;&lt;BR /&gt;
&lt;BR /&gt;
%count_vars(class)&lt;BR /&gt;
%put varnum = &amp;amp;varnum;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
HTH,&lt;BR /&gt;
Art</description>
    <pubDate>Sat, 30 Apr 2011 13:23:42 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2011-04-30T13:23:42Z</dc:date>
    <item>
      <title>SAS macro for counting the number of variables/columns in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-counting-the-number-of-variables-columns-in-a/m-p/60358#M13074</link>
      <description>Hi everyone, I can't figure out why this code doesn't work. Specificially, the &amp;amp;varnum doesn't get resolved.&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;options symbolgen;&lt;BR /&gt;
options mprint;&lt;BR /&gt;
options mlogic;&lt;BR /&gt;
%macro count_vars(dataset);&lt;BR /&gt;
	%local varnum;&lt;BR /&gt;
	proc sql;&lt;BR /&gt;
		select count(*) into : varnum&lt;BR /&gt;
		from dictionary.columns&lt;BR /&gt;
		where memname=upcase("&amp;amp;dataset");&lt;BR /&gt;
	quit;&lt;BR /&gt;
%mend count_vars;&lt;BR /&gt;
&lt;BR /&gt;
%count_vars(Mf_hist_holdings_all)&lt;BR /&gt;
%put varnum = &amp;amp;varnum;&lt;/B&gt;&lt;BR /&gt;
The ERROR message is like below:&lt;BR /&gt;
&lt;BR /&gt;
&lt;I&gt;&lt;B&gt;"MLOGIC(COUNT_VARS):  Ending execution.&lt;BR /&gt;
WARNING: Apparent symbolic reference VARNUM not resolved.&lt;BR /&gt;
421    %put varnum = &amp;amp;varnum;&lt;BR /&gt;
varnum = &amp;amp;varnum"&lt;/B&gt;&lt;/I&gt;&lt;BR /&gt;
&lt;BR /&gt;
By the way, I don't understand why this code doesn't run.&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;%macro count_vars(dataset, varnum);&lt;BR /&gt;
	proc sql;&lt;BR /&gt;
		select count(*) into : varnum&lt;BR /&gt;
		from dictionary.columns&lt;BR /&gt;
		where memname=upcase("&amp;amp;dataset");&lt;BR /&gt;
	quit;&lt;BR /&gt;
%mend count_vars;&lt;BR /&gt;
&lt;BR /&gt;
%count_vars(Mf_hist_holdings_all, varnum)&lt;/B&gt;&lt;BR /&gt;
&lt;BR /&gt;
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 &lt;B&gt;varnum&lt;/B&gt;. So when I clean a dataset, I can do the following&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;%global myvars;&lt;BR /&gt;
%count_vars(mydataset, myvars)&lt;/B&gt;&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;INSTEAD OF&lt;/B&gt; openning the source code and checking that the name is &lt;B&gt;varnum&lt;/B&gt; and only then can I put "varnum" as an &lt;B&gt;argument&lt;/B&gt; to the macro &lt;B&gt;%count_vars&lt;/B&gt;.&lt;BR /&gt;
&lt;BR /&gt;
Can somebody please advise?&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: smilingmelbourne

Message was edited by: smilingmelbourne</description>
      <pubDate>Sat, 30 Apr 2011 09:26:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-counting-the-number-of-variables-columns-in-a/m-p/60358#M13074</guid>
      <dc:creator>smilingmelbourne</dc:creator>
      <dc:date>2011-04-30T09:26:59Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro for counting the number of variables/columns in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-counting-the-number-of-variables-columns-in-a/m-p/60359#M13075</link>
      <description>Your first question is easy: you defined varnum as local!&lt;BR /&gt;
&lt;BR /&gt;
If you add a %put statement within the macro you will see that it does, in fact, get resolved.  E.g.,&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
%macro count_vars(dataset);&lt;BR /&gt;
%local varnum;&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  select count(*) into : varnum&lt;BR /&gt;
    from dictionary.columns&lt;BR /&gt;
      where memname=upcase("&amp;amp;dataset")&lt;BR /&gt;
  ;&lt;BR /&gt;
quit;&lt;BR /&gt;
%put varnum = &amp;amp;varnum;&lt;BR /&gt;
%mend count_vars;&lt;BR /&gt;
&lt;BR /&gt;
%count_vars(class)&lt;BR /&gt;
%put varnum = &amp;amp;varnum;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
HTH,&lt;BR /&gt;
Art</description>
      <pubDate>Sat, 30 Apr 2011 13:23:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-counting-the-number-of-variables-columns-in-a/m-p/60359#M13075</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2011-04-30T13:23:42Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro for counting the number of variables/columns in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-counting-the-number-of-variables-columns-in-a/m-p/60360#M13076</link>
      <description>Hi &lt;BR /&gt;
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.&lt;BR /&gt;
  &lt;BR /&gt;
where libname=%upcase("%scan(work.&amp;amp;dataset,-2)") and memname=%upcase("%scan(&amp;amp;dataset,-1)")&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick

P.S: What's the story of your name?&lt;BR /&gt;
&lt;A href="http://www.kitezh.com/texts/melbsmile.htm" target="_blank"&gt;http://www.kitezh.com/texts/melbsmile.htm&lt;/A&gt;  :-))&lt;BR /&gt;
&lt;BR /&gt;
    &lt;BR /&gt;
Message was edited by: Patrick</description>
      <pubDate>Sun, 01 May 2011 08:16:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-counting-the-number-of-variables-columns-in-a/m-p/60360#M13076</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2011-05-01T08:16:27Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro for counting the number of variables/columns in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-counting-the-number-of-variables-columns-in-a/m-p/60361#M13077</link>
      <description>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.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Tue, 03 May 2011 03:34:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-counting-the-number-of-variables-columns-in-a/m-p/60361#M13077</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-05-03T03:34:01Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro for counting the number of variables/columns in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-counting-the-number-of-variables-columns-in-a/m-p/60362#M13078</link>
      <description>Thank you all very much for the help. I've learnt a lot.</description>
      <pubDate>Tue, 03 May 2011 08:53:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-macro-for-counting-the-number-of-variables-columns-in-a/m-p/60362#M13078</guid>
      <dc:creator>smilingmelbourne</dc:creator>
      <dc:date>2011-05-03T08:53:43Z</dc:date>
    </item>
  </channel>
</rss>

