<?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 Macro using Symputx in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257373#M49399</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have what I thought was a simple macro&amp;nbsp;using&amp;nbsp;SYMPUTX to assign the&amp;nbsp;number of observations of a dataset to a macro variable. &amp;nbsp;My intent is to use this macro to&amp;nbsp;get the&amp;nbsp;observations numbers of multiple&amp;nbsp;datasets that are manipulated in various SAS programs, the resulting dataset would be printed for QC check and documentation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code. &amp;nbsp;In this example, I am using %put&amp;nbsp;to demonstrate the error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro ObsNumber(file,Var_Name);&lt;/P&gt;&lt;P&gt;data _NULL_;&lt;BR /&gt;if 0 then set &amp;amp;File. nobs=n;&lt;BR /&gt;call symputx(&amp;amp;Var_Name.,n);&lt;BR /&gt;stop;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%mend ObsNumber;&lt;/P&gt;&lt;P&gt;%ObsNumber(Sashelp.Class, 'Nrows_Class');&lt;/P&gt;&lt;P&gt;%put &amp;amp;Nrows_Class;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The log reads&lt;/P&gt;&lt;P&gt;WARNING: Apparent symbolic reference NROWS not resolved.&lt;BR /&gt;&amp;amp;Nrows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, If I pull the code out of the macro and run it with file and var_name manually entered, the log states 19 as expected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _NULL_;&lt;BR /&gt;if 0 then set Sashelp.Class nobs=n;&lt;BR /&gt;call symputx('Nrows',n);&lt;BR /&gt;stop;&lt;BR /&gt;run;&lt;BR /&gt;%put &amp;amp;Nrows;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any thoughts on a solution would be much appreciated. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks, Andrea&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 17 Mar 2016 17:18:20 GMT</pubDate>
    <dc:creator>simple_gearle</dc:creator>
    <dc:date>2016-03-17T17:18:20Z</dc:date>
    <item>
      <title>Macro using Symputx</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257373#M49399</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have what I thought was a simple macro&amp;nbsp;using&amp;nbsp;SYMPUTX to assign the&amp;nbsp;number of observations of a dataset to a macro variable. &amp;nbsp;My intent is to use this macro to&amp;nbsp;get the&amp;nbsp;observations numbers of multiple&amp;nbsp;datasets that are manipulated in various SAS programs, the resulting dataset would be printed for QC check and documentation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code. &amp;nbsp;In this example, I am using %put&amp;nbsp;to demonstrate the error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro ObsNumber(file,Var_Name);&lt;/P&gt;&lt;P&gt;data _NULL_;&lt;BR /&gt;if 0 then set &amp;amp;File. nobs=n;&lt;BR /&gt;call symputx(&amp;amp;Var_Name.,n);&lt;BR /&gt;stop;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%mend ObsNumber;&lt;/P&gt;&lt;P&gt;%ObsNumber(Sashelp.Class, 'Nrows_Class');&lt;/P&gt;&lt;P&gt;%put &amp;amp;Nrows_Class;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The log reads&lt;/P&gt;&lt;P&gt;WARNING: Apparent symbolic reference NROWS not resolved.&lt;BR /&gt;&amp;amp;Nrows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, If I pull the code out of the macro and run it with file and var_name manually entered, the log states 19 as expected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _NULL_;&lt;BR /&gt;if 0 then set Sashelp.Class nobs=n;&lt;BR /&gt;call symputx('Nrows',n);&lt;BR /&gt;stop;&lt;BR /&gt;run;&lt;BR /&gt;%put &amp;amp;Nrows;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any thoughts on a solution would be much appreciated. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks, Andrea&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Mar 2016 17:18:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257373#M49399</guid>
      <dc:creator>simple_gearle</dc:creator>
      <dc:date>2016-03-17T17:18:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro using Symputx</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257386#M49405</link>
      <description>&lt;P&gt;The problem is that the macro variable Nrows_class has local scope. Once the macro stops executing, the local symbol table is destroyed, and the value is no longer available. If your %put was INSIDE the macro, you could still access the Nrows_class variable:&lt;/P&gt;
&lt;PRE&gt;%macro ObsNumber(file,Var_Name);
   data _NULL_;
      if 0 then set &amp;amp;File. nobs=n;
      call symputx(&amp;amp;Var_Name.,n);
      stop;
   run;
   %put &amp;amp;Nrows_Class;
%mend ObsNumber;

%ObsNumber(Sashelp.Class, 'Nrows_Class');
&lt;/PRE&gt;
&lt;P&gt;produces the result you want in the log: 19.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need to access this value after the macro stops running, just use CALL SYMPUTX's third parameter to create the value in the global symbol table instead:&lt;/P&gt;
&lt;PRE&gt;%macro ObsNumber(file,Var_Name);
   data _NULL_;
      if 0 then set &amp;amp;File. nobs=n;
      call symputx(&amp;amp;Var_Name.,n,'G');
      stop;
   run;
%mend ObsNumber;

%ObsNumber(Sashelp.Class, 'Nrows_Class');

%put &amp;amp;Nrows_Class;&lt;/PRE&gt;
&lt;P&gt;And now you can access the value after the macros stops executing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Thu, 17 Mar 2016 21:09:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257386#M49405</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2016-03-17T21:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: Macro using Symputx</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257392#M49407</link>
      <description>Gee whiz, this does the trick. Thanks Mark!&lt;BR /&gt;</description>
      <pubDate>Thu, 17 Mar 2016 17:50:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257392#M49407</guid>
      <dc:creator>simple_gearle</dc:creator>
      <dc:date>2016-03-17T17:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: Macro using Symputx</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257398#M49411</link>
      <description>&lt;P&gt;Another option since you say you are looking printing the number would be to use Dictionary.Tables or sashelp.vtable with a where clause for the library and data set names and print the information directly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   title "SASHELP data sets starting with C";
   select memname, nobs
   from dictionary.tables 
   where libname='SASHELP' and memtype='DATA' and 
         substr(memname,1,1)='C'
   ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;even easier is all the datasets in a library or using IN operator a list of libraries. Or have a data set with the libname and member name and use a join to get the information. Note that lots of other information is available such as numbers of variable, size, dates, sorting, encoding.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Mar 2016 18:04:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257398#M49411</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-03-17T18:04:34Z</dc:date>
    </item>
    <item>
      <title>Re: Macro using Symputx</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257445#M49432</link>
      <description>That is really slick code for getting at all of the data sets in library. I may only need the previous code for getting any temporary datafiles. Thanks for your help!&lt;BR /&gt;</description>
      <pubDate>Thu, 17 Mar 2016 20:28:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257445#M49432</guid>
      <dc:creator>simple_gearle</dc:creator>
      <dc:date>2016-03-17T20:28:07Z</dc:date>
    </item>
    <item>
      <title>Re: Macro using Symputx</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257449#M49434</link>
      <description>&lt;P&gt;Since temporary sets go into WORK, the use WORK for the library name and the example code will get sizes for your temp sets as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Mar 2016 20:38:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257449#M49434</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-03-17T20:38:19Z</dc:date>
    </item>
    <item>
      <title>Re: Macro using Symputx</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257459#M49435</link>
      <description>My pleasure &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Thu, 17 Mar 2016 21:07:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257459#M49435</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2016-03-17T21:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: Macro using Symputx</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257461#M49436</link>
      <description>&lt;P&gt;A couple of other coding points on your code.&lt;/P&gt;
&lt;P&gt;1) Pass the name of the macro variable without the quotes around it. &amp;nbsp;You can use double quotes in the CALL SYMPUTX() statement to expand the macro variable's value as the name of the target macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ObsNumber(file,Var_Name);
...
call symputx("&amp;amp;Var_Name",n);
...
%mend ObsNumber;
%ObsNumber(Sashelp.Class, Nrows_Class);
%put &amp;amp;Nrows_Class;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2) You don't need the IF 0 trick since you have already added a hard coded STOP statement to your data step. Just change the order of the statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  call symputx("&amp;amp;Var_Name",n);
  stop;
  set &amp;amp;File. nobs=n;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Mar 2016 21:28:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257461#M49436</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-03-17T21:28:50Z</dc:date>
    </item>
    <item>
      <title>Re: Macro using Symputx</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257723#M49517</link>
      <description>&lt;P&gt;You can avoid running a data step to get this information&lt;/P&gt;&lt;P&gt;by using sysfunc to call scl functions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.sascommunity.org/wiki/Macro_nobs" target="_blank"&gt;http://www.sascommunity.org/wiki/Macro_nobs&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ron Fehd&amp;nbsp; macro maven&lt;/P&gt;</description>
      <pubDate>Sat, 19 Mar 2016 00:14:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-using-Symputx/m-p/257723#M49517</guid>
      <dc:creator>Ron_MacroMaven</dc:creator>
      <dc:date>2016-03-19T00:14:04Z</dc:date>
    </item>
  </channel>
</rss>

