<?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 VAR in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/907907#M358357</link>
    <description>&lt;P&gt;There is little practical advantage of Way 1 vs Way 2, other than clarity of code / purpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this step you don't have to read any data from sashelp.class.&amp;nbsp; When the SET statement compiles, it will assign the value to n.&amp;nbsp; So the SET statement does not need to execute.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With Way 1, the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set ... ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is a way to prevent the SET statement from executing.&amp;nbsp; It's a usual IF THEN statement.&amp;nbsp; IF 0 is false, so the SET statement never executes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's common to use IF 0 then SET in a case where you don't want to read data from a dataset, but you do want metadata (e.g. variable names, or nobs in this case) the DATA step compiles.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In practice, Way 1 will not read any date from sashelp.class. Way 2 will read just one record from sashelp.class, because the STOP statement will end execution of the DATA step before the second record is read.&amp;nbsp; So you won't see a noticeable difference in execution time between the two methods.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Still, Way 1 is generally preferable, as IF 0 THEN SET is a familiar recognizable coding pattern, that will make it easier for others to read your code.&lt;/P&gt;</description>
    <pubDate>Wed, 13 Dec 2023 19:34:04 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2023-12-13T19:34:04Z</dc:date>
    <item>
      <title>Macro VAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/907889#M358354</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I saw 2 ways to create a macro var with value of number of observations.&lt;/P&gt;
&lt;P&gt;My question- What is the advantage of way 1 over way 2?&lt;/P&gt;
&lt;P&gt;I see that both ways work but way2 is easier code.&lt;/P&gt;
&lt;P&gt;What does it mean "&lt;CODE class=" language-sas"&gt;if 0 "?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/***Way1***/
data _null_;
if 0 then set sashelp.class nobs=n;
call symput('num_obs',n);
stop;
run;
%put &amp;amp;num_obs;

/***Way2***/
data _null_;
set sashelp.class nobs=n;
call symput('nr_obs',n);
stop;
run;
%put &amp;amp;nr_obs;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Dec 2023 18:55:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/907889#M358354</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-12-13T18:55:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro VAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/907898#M358356</link>
      <description>&lt;P&gt;"Way 2" will pull every observation and repeatedly assign the same value to the macro variable. If the data set is millions of observations it could take quite a while and waste lots of CPU. "Way 1" will take about the same amount of time regardless of the number of observations.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 19:14:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/907898#M358356</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-12-13T19:14:14Z</dc:date>
    </item>
    <item>
      <title>Re: Macro VAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/907907#M358357</link>
      <description>&lt;P&gt;There is little practical advantage of Way 1 vs Way 2, other than clarity of code / purpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this step you don't have to read any data from sashelp.class.&amp;nbsp; When the SET statement compiles, it will assign the value to n.&amp;nbsp; So the SET statement does not need to execute.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With Way 1, the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set ... ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is a way to prevent the SET statement from executing.&amp;nbsp; It's a usual IF THEN statement.&amp;nbsp; IF 0 is false, so the SET statement never executes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's common to use IF 0 then SET in a case where you don't want to read data from a dataset, but you do want metadata (e.g. variable names, or nobs in this case) the DATA step compiles.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In practice, Way 1 will not read any date from sashelp.class. Way 2 will read just one record from sashelp.class, because the STOP statement will end execution of the DATA step before the second record is read.&amp;nbsp; So you won't see a noticeable difference in execution time between the two methods.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Still, Way 1 is generally preferable, as IF 0 THEN SET is a familiar recognizable coding pattern, that will make it easier for others to read your code.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 19:34:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/907907#M358357</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-12-13T19:34:04Z</dc:date>
    </item>
    <item>
      <title>Re: Macro VAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/907924#M358358</link>
      <description>&lt;P&gt;Way 1 will not read an observation, way 2 will read the first. Since that obs is contained in the dataset header page, which has to be read physically anyway, the performance difference will be negligible.&lt;/P&gt;
&lt;P&gt;So it's up to what is easier to understand for the next coder who comes across the program.&lt;/P&gt;
&lt;P&gt;But for this, I'd prefer&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select nobs into :num_obs
from dictionary.tables
where libname = "SASHELP" and memname = "CLASS";
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Dec 2023 19:50:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/907924#M358358</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-12-13T19:50:57Z</dc:date>
    </item>
    <item>
      <title>Re: Macro VAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/907925#M358359</link>
      <description>&lt;P&gt;Use symputx not symput. The former is designed to do automatic numeric to character conversions.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 19:55:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/907925#M358359</guid>
      <dc:creator>WarrenKuhfeld</dc:creator>
      <dc:date>2023-12-13T19:55:03Z</dc:date>
    </item>
    <item>
      <title>Re: Macro VAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/907927#M358360</link>
      <description>&lt;P&gt;Since you have a STOP statement you don't need the IF/THEN.&amp;nbsp; Just move the SET after the STOP.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  call symputx('num_obs',n);
  stop;
  set sashelp.class nobs=n;
run;
%put &amp;amp;num_obs;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS Do not use the ancient CALL SYMPUT() method unless you actually have a need to generate macro variables that contain leading and/or trailing spaces.&amp;nbsp; It was replaced by CALL SYMPUTX() decades ago.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2023 20:01:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/907927#M358360</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-13T20:01:52Z</dc:date>
    </item>
    <item>
      <title>Re: Macro VAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/908149#M358423</link>
      <description>&lt;P&gt;The first method (or the even simpler code shown by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;) is the best. The second method will not execute the SYMPUT statement if the number of observations is 0, which may cause problems later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And yes, as others have already suggested, use SYMPUTX, not SYMPUT. In the actual example, the difference will be that the SYMPUT-ed variable will contain trailing blanks, the other will not.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 08:24:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/908149#M358423</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-12-15T08:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: Macro VAR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/908150#M358424</link>
      <description>&lt;P&gt;One additional thing to consider: the DATA step methods will fail with an ERROR if the dataset does not exist, the SQL query will simply not return a value. Depending on context, the SQL method might be an easier way to deal gracefully with a missing dataset (particularly important for batch jobs which should continue executing).&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 08:49:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-VAR/m-p/908150#M358424</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-12-15T08:49:06Z</dc:date>
    </item>
  </channel>
</rss>

