<?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: Why 2 call symput stmts are needed to create a macro variable based on the row counts of a table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Why-2-call-symput-stmts-are-needed-to-create-a-macro-variable/m-p/918842#M361932</link>
    <description>I see. This is very helpful, really appreciated for your advice.</description>
    <pubDate>Mon, 04 Mar 2024 15:33:26 GMT</pubDate>
    <dc:creator>LL5</dc:creator>
    <dc:date>2024-03-04T15:33:26Z</dc:date>
    <item>
      <title>Why 2 call symput stmts are needed to create a macro variable based on the row counts of a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-2-call-symput-stmts-are-needed-to-create-a-macro-variable/m-p/918827#M361925</link>
      <description>&lt;P&gt;The below code is to create a macro variable calls ROWCNT based on the number of observations from a table named MY_TABLE. The code worked and I understand what the second Call Symput statement does.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I try to get a full understanding on why we need to create "ROWCNT" macro variable and default to 0 before reading MY_TABLE. It'd be greatly appreciated if anyone can share some insights.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;DATA _NULL_;
	CALL SYMPUT('ROWCNT',0);
	SET WORK.MY_TABLE;
	CALL SYMPUT('ROWCNT',_N_);
	RUN;
	%PUT &amp;amp;ROWCNT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Mar 2024 14:09:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-2-call-symput-stmts-are-needed-to-create-a-macro-variable/m-p/918827#M361925</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2024-03-04T14:09:12Z</dc:date>
    </item>
    <item>
      <title>Re: Why 2 call symput stmts are needed to create a macro variable based on the row counts of a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-2-call-symput-stmts-are-needed-to-create-a-macro-variable/m-p/918832#M361926</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/89004"&gt;@LL5&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The below code is to create a macro variable calls ROWCNT based on the number of observations from a table named MY_TABLE. The code worked and I understand what the second Call Symput statement does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I try to get a full understanding on why we need to create "ROWCNT" macro variable and default to 0 before reading MY_TABLE. It'd be greatly appreciated if anyone can share some insights.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;DATA _NULL_;
	CALL SYMPUT('ROWCNT',0);
	SET WORK.MY_TABLE;
	CALL SYMPUT('ROWCNT',_N_);
	RUN;
	%PUT &amp;amp;ROWCNT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;STRONG&gt;It is needed to handle the case when MY_TABLE is empty.&lt;/STRONG&gt;&amp;nbsp; When that happens the data step will end at the SET statement and the second CALL SYMPUT() will never be called.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also &lt;STRONG&gt;never use the ANCIENT function CALL SYMPUT()&lt;/STRONG&gt;, it was supplanted by the more powerful CALL SYMPUTX() over 20 years ago.&amp;nbsp; The only reason to ever use CALL SYMPUT() is if you require the generated macro variable to contain leading and/or trailing spaces.&amp;nbsp; Something you DO NOT want for this application.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can replace the first CALL with a simple %LET statement instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let rowcnt=0;
DATA _NULL_;
  SET WORK.MY_TABLE;
  CALL SYMPUTX('ROWCNT',_N_);
RUN;
%PUT &amp;amp;=ROWCNT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And since you are referencing and actual SAS dataset (and not a view or an external database) then use the NOBS= option of the SET statement SAS will count the obs for you and you can save a lot of time by not reading the whole dataset.&amp;nbsp; Then you don't need the extra assignment to the macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA _NULL_;
  CALL SYMPUTX('ROWCNT',nobs);
  stop;
  SET WORK.MY_TABLE nobs=nobs;
RUN;
%PUT &amp;amp;=ROWCNT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if you do have to read all of the observations you can use the END= option of the SET statement to know when to generate the macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA _NULL_;
  if eof then CALL SYMPUTX('ROWCNT',_n_-1);
  SET WORK.MY_TABLE end=eof;
RUN;
%PUT &amp;amp;=ROWCNT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Mar 2024 14:31:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-2-call-symput-stmts-are-needed-to-create-a-macro-variable/m-p/918832#M361926</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-04T14:31:45Z</dc:date>
    </item>
    <item>
      <title>Re: Why 2 call symput stmts are needed to create a macro variable based on the row counts of a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-2-call-symput-stmts-are-needed-to-create-a-macro-variable/m-p/918838#M361929</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;, thanks a lot for this explanation. May I ask what does the stop statement do after the call symputx and before the set statement in your second to last example?</description>
      <pubDate>Mon, 04 Mar 2024 15:03:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-2-call-symput-stmts-are-needed-to-create-a-macro-variable/m-p/918838#M361929</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2024-03-04T15:03:25Z</dc:date>
    </item>
    <item>
      <title>Re: Why 2 call symput stmts are needed to create a macro variable based on the row counts of a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-2-call-symput-stmts-are-needed-to-create-a-macro-variable/m-p/918839#M361930</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/89004"&gt;@LL5&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;, thanks a lot for this explanation. May I ask what does the stop statement do after the call symputx and before the set statement in your second to last example?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Just what it sounds like.&amp;nbsp; It stops the data step at that point.&amp;nbsp; That way you don't actually have to ever execute the SET statement.&amp;nbsp; The count was already put into the NOBS variable during the compile/setup process of the data step so you only need run the CALL SYMPUTX() function and nothing else.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2024 15:09:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-2-call-symput-stmts-are-needed-to-create-a-macro-variable/m-p/918839#M361930</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-04T15:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: Why 2 call symput stmts are needed to create a macro variable based on the row counts of a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-2-call-symput-stmts-are-needed-to-create-a-macro-variable/m-p/918842#M361932</link>
      <description>I see. This is very helpful, really appreciated for your advice.</description>
      <pubDate>Mon, 04 Mar 2024 15:33:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-2-call-symput-stmts-are-needed-to-create-a-macro-variable/m-p/918842#M361932</guid>
      <dc:creator>LL5</dc:creator>
      <dc:date>2024-03-04T15:33:26Z</dc:date>
    </item>
  </channel>
</rss>

