<?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: How to put these code lines info a MACRO?! in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-put-these-code-lines-info-a-MACRO/m-p/985314#M379821</link>
    <description>&lt;P&gt;I would prefer to use this :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ds_rowct(ds);
%let dsid=%sysfunc(open(&amp;amp;ds.));
%let nobs=%sysfunc(attrn(&amp;amp;dsid.,nlobs));
%let dsid=%sysfunc(close(&amp;amp;dsid.));

%put &amp;amp;ds. have &amp;amp;nobs. obs.;
%mend; 



%ds_rowct(sashelp.class)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1    %macro ds_rowct(ds);
2    %let dsid=%sysfunc(open(&amp;amp;ds.));
3    %let nobs=%sysfunc(attrn(&amp;amp;dsid.,nlobs));
4    %let dsid=%sysfunc(close(&amp;amp;dsid.));
5
6    %put &amp;amp;ds. have &amp;amp;nobs. obs.;
7    %mend;
8
9
10
11   %ds_rowct(sashelp.class)
sashelp.class have 19 obs.

&lt;/PRE&gt;</description>
    <pubDate>Wed, 25 Mar 2026 06:50:31 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2026-03-25T06:50:31Z</dc:date>
    <item>
      <title>How to put these code lines info a MACRO?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-put-these-code-lines-info-a-MACRO/m-p/985308#M379815</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data _null_; 
	*i=1; 
	if i = 0 then set &amp;amp;ds. nobs= mycount; 
	Call symput('mycount', mycount); 
	Run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "mycount" macro variable keeps the dataset row count.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How to put these lines into a macro which returns the dataset row count?!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ds_rowct(ds);
....

%mend;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Mar 2026 02:58:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-put-these-code-lines-info-a-MACRO/m-p/985308#M379815</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2026-03-25T02:58:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to put these code lines info a MACRO?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-put-these-code-lines-info-a-MACRO/m-p/985309#M379816</link>
      <description>&lt;P&gt;Macros do not normally "return" anything.&amp;nbsp; The just generate SAS code for SAS to execute.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the simplest way to have it "return" something is to TELL it WHERE to put what you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ds_rowct(ds,mvar=mycount);
%if not %symexist(&amp;amp;mvar) %then %global &amp;amp;mvar;
data _null_; 
  call symputx("&amp;amp;mvar",mycount);
  stop;
  set &amp;amp;ds. nobs=mycount; 
run;
%mend; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt; 82         %ds_rowct(sashelp.class)
 
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
       
 
 83         %put &amp;amp;=mycount;
 MYCOUNT=19&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS: You almost never want to use the ancient CALL SYMPUT() method.&amp;nbsp; The CALL SYMPUTX() method replaced it decades ago.&amp;nbsp; The only time you would want CALL SYMPUT() is when you need the macro variable's value to have leading or trailing spaces.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Mar 2026 03:12:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-put-these-code-lines-info-a-MACRO/m-p/985309#M379816</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-03-25T03:12:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to put these code lines info a MACRO?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-put-these-code-lines-info-a-MACRO/m-p/985312#M379819</link>
      <description>&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If mvar pre-exists, I assume still work.&amp;nbsp; But anyway, no additional macro variable?!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro does not return anything. But much more convenient to "Take" that way , say,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if %ds_rowct(&amp;amp;dsname.) %then %do;&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;If not, simply invoke the macro and then evaluate &lt;U&gt;the speific macro variable, BUT&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;the reality is that too hard to remember all the variables when you are coding with&amp;nbsp;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;dozens of macros!!!&lt;/U&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Mar 2026 04:41:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-put-these-code-lines-info-a-MACRO/m-p/985312#M379819</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2026-03-25T04:41:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to put these code lines info a MACRO?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-put-these-code-lines-info-a-MACRO/m-p/985314#M379821</link>
      <description>&lt;P&gt;I would prefer to use this :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ds_rowct(ds);
%let dsid=%sysfunc(open(&amp;amp;ds.));
%let nobs=%sysfunc(attrn(&amp;amp;dsid.,nlobs));
%let dsid=%sysfunc(close(&amp;amp;dsid.));

%put &amp;amp;ds. have &amp;amp;nobs. obs.;
%mend; 



%ds_rowct(sashelp.class)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1    %macro ds_rowct(ds);
2    %let dsid=%sysfunc(open(&amp;amp;ds.));
3    %let nobs=%sysfunc(attrn(&amp;amp;dsid.,nlobs));
4    %let dsid=%sysfunc(close(&amp;amp;dsid.));
5
6    %put &amp;amp;ds. have &amp;amp;nobs. obs.;
7    %mend;
8
9
10
11   %ds_rowct(sashelp.class)
sashelp.class have 19 obs.

&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Mar 2026 06:50:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-put-these-code-lines-info-a-MACRO/m-p/985314#M379821</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2026-03-25T06:50:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to put these code lines info a MACRO?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-put-these-code-lines-info-a-MACRO/m-p/985351#M379828</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/409584"&gt;@hellohere&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If mvar pre-exists, I assume still work.&amp;nbsp; But anyway, no additional macro variable?!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro does not return anything. But much more convenient to "Take" that way , say,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if %ds_rowct(&amp;amp;dsname.) %then %do;&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;If not, simply invoke the macro and then evaluate &lt;U&gt;the speific macro variable, BUT&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;the reality is that too hard to remember all the variables when you are coding with&amp;nbsp;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;dozens of macros!!!&lt;/U&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can create macros that only emit PART of a statement.&amp;nbsp; Then you could use the macro as if it was a function, like in your new example.&amp;nbsp; Note that your original question did not state that was your objective.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are dozens of examples of macros that do that for the example problem of finding the number of observations in a dataset.&amp;nbsp; See for example this one:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/sasutils/macros/blob/master/nobs.sas" target="_blank" rel="noopener"&gt;https://github.com/sasutils/macros/blob/master/nobs.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if 0&amp;lt;%nobs(&amp;amp;dsname.,mvar=) %then %do;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Mar 2026 14:19:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-put-these-code-lines-info-a-MACRO/m-p/985351#M379828</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-03-25T14:19:48Z</dc:date>
    </item>
  </channel>
</rss>

