<?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: Macro that counts the number of rows in a data table. Understanding how SAS works in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551108#M153092</link>
    <description>&lt;P&gt;Also, you don't need a macro to do stuff like this &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _NULL_;
	if 0 then set sashelp.class nobs=n;
	call symputx('nrows',n);
	stop;
run;

%put nobs=&amp;amp;nrows;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 15 Apr 2019 15:52:59 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-04-15T15:52:59Z</dc:date>
    <item>
      <title>Macro that counts the number of rows in a data table. Understanding how SAS works</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551092#M153086</link>
      <description>&lt;P&gt;Hello everyone,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;I haven't been able to find predefined functions that will work directly on SAS as ncol() or nrow() in R for a dataset.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Therefore, I have tried to replicate the function nrow() using the following logic, trying to store the result in a variable.&lt;/P&gt;&lt;P&gt;My attempt has been as following:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%let CARS= sashelp.cars ; * Storing the predefined dataset in a variable

%macro nrow(TableName);
proc sql;
	select count(*)
	from &amp;amp;TableName;
	quit; 
%mend;

%let res =%nrow(&amp;amp;CARS); * Storing the result of the macro in the variable;
%put &amp;amp;res; * I assume this is like printing (I have read that it passes the number to char also);

&lt;/PRE&gt;&lt;P&gt;However, that does not work.&lt;/P&gt;&lt;P&gt;(1) Can anyone help me out with it respecting the idea of how I have written it (if possible)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would appreciate a clear explanation on my mistake rather than a sole correction.&lt;/P&gt;&lt;P&gt;Thank you for your time in advanced.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 15:38:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551092#M153086</guid>
      <dc:creator>carles</dc:creator>
      <dc:date>2019-04-15T15:38:43Z</dc:date>
    </item>
    <item>
      <title>Re: Macro that counts the number of rows in a data table. Understanding how SAS works</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551103#M153088</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro nrows(tablename);
%global nrows;
proc sql;
select nobs into :nrows from dictionary.tables  where libname="WORK" and memname="%upcase(&amp;amp;tablename)";
quit;
%mend;

ods listing;
%nrows(abc)

%put &amp;amp;=nrows;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can make obvious modifications for data sets that are not in the WORK library.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 15:50:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551103#M153088</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-04-15T15:50:28Z</dc:date>
    </item>
    <item>
      <title>Re: Macro that counts the number of rows in a data table. Understanding how SAS works</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551105#M153089</link>
      <description>Keeping it simple:&lt;BR /&gt;&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;	select count(*) into :recs &lt;BR /&gt;	from sashelp.cars;&lt;BR /&gt;	quit; &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;%put &amp;amp;=recs;</description>
      <pubDate>Mon, 15 Apr 2019 15:49:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551105#M153089</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2019-04-15T15:49:48Z</dc:date>
    </item>
    <item>
      <title>Re: Macro that counts the number of rows in a data table. Understanding how SAS works</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551106#M153090</link>
      <description>&lt;P&gt;Hi and welcome to the SAS Communities &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's not that I don't want to help you with your macro. However, if you really want to understand SAS, I don't think you should compare it directly to R. A SAS data set is not the same think as a matrix. SAS has an entire language called &lt;A href="https://documentation.sas.com/?docsetId=imlug&amp;amp;docsetTarget=titlepage.htm&amp;amp;docsetVersion=14.3&amp;amp;locale=da" target="_self"&gt;IML&lt;/A&gt; (Interactive Matrix Language) that is the closest you get to R in a SAS context. Here, the NROW and NCOL Functions do exist and does basically the same thing as in R.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My recommendation is to take the &lt;A href="https://vle.sas.com/course/view.php?name=EPG194" target="_self"&gt;SAS Programming 1: Essentials&lt;/A&gt; course, provided for free by SAS Institute. It will get you going on how the SAS programming language is structured.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are a Matrix Kind of Guy/Girl, then check out IML. The book&amp;nbsp;&lt;A href="https://www.sas.com/store/books/categories/usage-and-reference/statistical-programming-with-sas-iml-software/prodBK_63119_en.html" target="_self"&gt;Statistical Programming with SAS/IML® Software&lt;/A&gt; by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;is the way to go.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards Peter&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 15:50:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551106#M153090</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-04-15T15:50:02Z</dc:date>
    </item>
    <item>
      <title>Re: Macro that counts the number of rows in a data table. Understanding how SAS works</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551108#M153092</link>
      <description>&lt;P&gt;Also, you don't need a macro to do stuff like this &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _NULL_;
	if 0 then set sashelp.class nobs=n;
	call symputx('nrows',n);
	stop;
run;

%put nobs=&amp;amp;nrows;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Apr 2019 15:52:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551108#M153092</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-04-15T15:52:59Z</dc:date>
    </item>
    <item>
      <title>Re: Macro that counts the number of rows in a data table. Understanding how SAS works</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551114#M153096</link>
      <description>&lt;P&gt;To expand on Draycut's&amp;nbsp;suggestion:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
 /* if you want a matrix of numeric vars */
use sashelp.cars;
read all var _num_ into X;
close;

n = nrow(X);
print n;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Apr 2019 16:03:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551114#M153096</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-04-15T16:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: Macro that counts the number of rows in a data table. Understanding how SAS works</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551130#M153106</link>
      <description>&lt;P&gt;You cannot treat everything in SAS as if it is a function like you can in R.&lt;/P&gt;
&lt;P&gt;In particular SAS macro code is just that, a macro processor, and not a language. It just manipulates text to make it easier to generate the code you want to run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you have a macro that generates a PROC step you cannot "assign" it to a macro variable. Because once the macro call has been converted into the text that it generates instead of running that text as code you will just end up assigning that text to the macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example the text that your macro generates starts with:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; select ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So if you use that in a %LET statement to set a macro variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let res=%nrow(sashelp.class);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You end up generating this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let res=proc sql;
select ...;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;So the macro variable RES now has the string 'proc sql' and the SELECT statement is trying run outside of a PROC SQL step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to make a macro that return a single value, but requires to run SAS code to do it then write the value to a macro variable. You can ask the caller to tell you want macro variable to create.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro nrow(TableName,mvar);
%if not %symexist(&amp;amp;mvar) %then %global &amp;amp;mvar;
proc sql noprint;
  select count(*) into :&amp;amp;mvar trimmed
    from &amp;amp;TableName
  ;
quit; 
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then your call would look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%nrow(sashelp.class,res);
%put Macro variable RES has value &amp;amp;res;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 16:25:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551130#M153106</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-04-15T16:25:45Z</dc:date>
    </item>
    <item>
      <title>Re: Macro that counts the number of rows in a data table. Understanding how SAS works</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551308#M153164</link>
      <description>&lt;P&gt;Thank you Peter,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will start today reading the links you sent me.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 06:22:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-that-counts-the-number-of-rows-in-a-data-table/m-p/551308#M153164</guid>
      <dc:creator>carles</dc:creator>
      <dc:date>2019-04-16T06:22:21Z</dc:date>
    </item>
  </channel>
</rss>

