<?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: Creating and Using Macro within Same Step in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Creating-and-Using-Macro-within-Same-Step/m-p/944088#M42425</link>
    <description>&lt;P&gt;In your macro call you asked the macro to put the count into a macro variable named SALESCOUNT.&amp;nbsp; If you want to use SALESCOUNT after the macro ends then make sure to define it BEFORE calling the macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let salescount=unknown;
%counts(userid, salescount, customers)
%put &amp;amp;=salescount;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You named the parameter COUNT, but I would think a name like MVAR or MVARNAME would have been better. It would make it clearer how the parameter is going to be used.&amp;nbsp; You can make the macro smart enough to force the macro variable into the global symbol table when it does not already exist.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro counts (varname, mvarname, dsname);
%if not %symexist(&amp;amp;mvarname) %then %global &amp;amp;mvarname;
proc sql;
select count(distinct &amp;amp;varname) format=32. into :&amp;amp;mvarname trimmed
from &amp;amp;dsname;
quit;
%mend counts;

%counts(dsname=customers,varname=userid, mvarname=salescount)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 16 Sep 2024 14:24:33 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-09-16T14:24:33Z</dc:date>
    <item>
      <title>Creating and Using Macro within Same Step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-and-Using-Macro-within-Same-Step/m-p/944071#M42421</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello, I'm trying to utilize the macro below to prevent typing out this full sql query many times throughout the code. The into :&amp;amp;count is causing issues. Although it doesn't show an error, later when I call salescount, it's clear the macro was not assigned properly "Apparent symbolic reference salescount was not resolved." Is there a way to fix the macro or will I have to go back to writing each proc sql&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%macro counts (variable1, count, dataname);

proc sql;
select count(distinct &amp;amp;variable1) into :&amp;amp;count trimmed
from &amp;amp;dataname;
quit;
%mend counts;

%counts(userid, salescount, customers)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2024 13:23:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-and-Using-Macro-within-Same-Step/m-p/944071#M42421</guid>
      <dc:creator>sasser27</dc:creator>
      <dc:date>2024-09-16T13:23:36Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and Using Macro within Same Step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-and-Using-Macro-within-Same-Step/m-p/944075#M42422</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/469638"&gt;@sasser27&lt;/a&gt;, there's a few ways to proceed here. You could provide a sample customers dataset (just a few observations) for us to test your macro or you could test out the SQL query by itself to see if the result is as expected before encapsulating it in a macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've tested out the macro with a simple customers table that I created:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data customers;
	input userid;
	format userid z3.;
datalines;
001
002
003
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and the macro worked and &amp;amp;salescount returned the number 3 in the log, as expected.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;80   %put &amp;amp;salescount;
3
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Global system options MPRINT and SYMBOLGEN are also useful when working with macros to be able to trace what a macro is doing:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint symbolgen;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Sep 2024 13:54:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-and-Using-Macro-within-Same-Step/m-p/944075#M42422</guid>
      <dc:creator>antonbcristina</dc:creator>
      <dc:date>2024-09-16T13:54:39Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and Using Macro within Same Step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-and-Using-Macro-within-Same-Step/m-p/944077#M42423</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; The main issue you're having is that the macro variable is in the local scope, which means that after the macro program executes, that variable is "cleaned up" with the rest of the local macro variables used within the macro program. So in order to be able to use your macro variable from the PROC&amp;nbsp; SQL after the macro ends, you need to declare at least the &amp;amp;count macro variable value as a GLOBAL macro variable:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1726495317097.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100383i777C499BD8D13899/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1726495317097.png" alt="Cynthia_sas_0-1726495317097.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2024 14:03:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-and-Using-Macro-within-Same-Step/m-p/944077#M42423</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2024-09-16T14:03:12Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and Using Macro within Same Step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-and-Using-Macro-within-Same-Step/m-p/944079#M42424</link>
      <description>&lt;P&gt;Your example is incomplete because it does not show anything that would generate the "Apparent symbolic reference salescount was not resolved." message where you attempt to use the Salescount variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Part of the issue is that the value of &amp;amp;count in the macro is local in scope, that means it only exists inside the macro Counts and code created/used there.&lt;/P&gt;
&lt;P&gt;The fix would be to make the macro variable global:&lt;/P&gt;
&lt;PRE&gt;%macro counts (variable1, count, dataname);
%global &amp;amp;count;
proc sql;
select count(distinct &amp;amp;variable1) into :&amp;amp;count trimmed
from &amp;amp;dataname;
quit;
%mend counts;&lt;/PRE&gt;
&lt;P&gt;And an example that uses the macro with a data set you can test &lt;STRONG&gt;and&lt;/STRONG&gt; uses the created macro variable holding the count:&lt;/P&gt;
&lt;PRE&gt;%counts (name,namecount,sashelp.class);

%put Namecount is "&amp;amp;namecount";&lt;/PRE&gt;
&lt;P&gt;Which does return the expected value of 19&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2024 14:10:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-and-Using-Macro-within-Same-Step/m-p/944079#M42424</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-09-16T14:10:03Z</dc:date>
    </item>
    <item>
      <title>Re: Creating and Using Macro within Same Step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-and-Using-Macro-within-Same-Step/m-p/944088#M42425</link>
      <description>&lt;P&gt;In your macro call you asked the macro to put the count into a macro variable named SALESCOUNT.&amp;nbsp; If you want to use SALESCOUNT after the macro ends then make sure to define it BEFORE calling the macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let salescount=unknown;
%counts(userid, salescount, customers)
%put &amp;amp;=salescount;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You named the parameter COUNT, but I would think a name like MVAR or MVARNAME would have been better. It would make it clearer how the parameter is going to be used.&amp;nbsp; You can make the macro smart enough to force the macro variable into the global symbol table when it does not already exist.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro counts (varname, mvarname, dsname);
%if not %symexist(&amp;amp;mvarname) %then %global &amp;amp;mvarname;
proc sql;
select count(distinct &amp;amp;varname) format=32. into :&amp;amp;mvarname trimmed
from &amp;amp;dsname;
quit;
%mend counts;

%counts(dsname=customers,varname=userid, mvarname=salescount)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2024 14:24:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-and-Using-Macro-within-Same-Step/m-p/944088#M42425</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-16T14:24:33Z</dc:date>
    </item>
  </channel>
</rss>

