<?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 UNDERSTANDING OF THE CODE in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860617#M38023</link>
    <description>&lt;P&gt;I was just trying to learn SAS more thoroughly, I came across the below code can anyone explain what does this code is performing. (much appreciated if you could do a line by line explanation except data and set. xD)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymacro(k);
data want;
set temp;
%do i = 1 %to &amp;amp;k;
if _N_ = &amp;amp;i then y = %eval(&amp;amp;i.* 10);
%end;
run;
%mend;
data _null_;
call execute ('%mymacro(6)');run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Kindly, help.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance. Much Appreciated. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 24 Feb 2023 08:45:12 GMT</pubDate>
    <dc:creator>Kirito1</dc:creator>
    <dc:date>2023-02-24T08:45:12Z</dc:date>
    <item>
      <title>UNDERSTANDING OF THE CODE</title>
      <link>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860617#M38023</link>
      <description>&lt;P&gt;I was just trying to learn SAS more thoroughly, I came across the below code can anyone explain what does this code is performing. (much appreciated if you could do a line by line explanation except data and set. xD)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymacro(k);
data want;
set temp;
%do i = 1 %to &amp;amp;k;
if _N_ = &amp;amp;i then y = %eval(&amp;amp;i.* 10);
%end;
run;
%mend;
data _null_;
call execute ('%mymacro(6)');run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Kindly, help.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance. Much Appreciated. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 08:45:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860617#M38023</guid>
      <dc:creator>Kirito1</dc:creator>
      <dc:date>2023-02-24T08:45:12Z</dc:date>
    </item>
    <item>
      <title>Re: UNDERSTANDING OF THE CODE</title>
      <link>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860624#M38024</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First of of all, this code is just an ugly version of:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; 
  set temp; 
  if _N_ &amp;lt;= 6 then y = _N_*10; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but lets got through it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Starting from the end:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data _null_;
call execute ('%mymacro(6)');run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;this part is running call execute() function to run macro "mymacro" defined just before that data step. Such code can be justified in a situation when you want to use data from some dataset as input parameters for some macro e.g., a dataset contains variable "date" and you want to run a macro for each value of "date" from that dataset. In case of the code we are looking at it could be just replaced with simple:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;%mymacro(6)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The part:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;%macro mymacro(k);
...
%mend;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;defines a macro named "mymacro". Macros, in short words, are the way to make (or rather "add to") static 4GL language more dynamics. Or, another "short", macros are "code which writes code".&lt;/P&gt;
&lt;P&gt;Inside the macro definition you can see both 4GL code and macro code. 4GL code is for example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data want;
set temp;

if _N_ =  then y = ;

run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and macro code is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;%do i = 1 %to &amp;amp;k;
         &amp;amp;i       %eval(&amp;amp;i.* 10);
%end;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The %do-loop is a macro loop which will repeat and generate text which is in its body i.e.,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;if _N_ = &amp;amp;i then y = %eval(&amp;amp;i.* 10);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(so mix of 4GL and macro language) into pure 4GL. For example the first iteration will give you:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;if _N_ = 1 then y = 10;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and the 5th iteration will give:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;if _N_ = 5 then y = 50;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;so the "final code generated by the macro", and executed for value 6 will be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; 
set temp; 
if _N_ = 1 then y = 10; 
if _N_ = 2 then y = 20; 
if _N_ = 3 then y = 30; 
if _N_ = 4 then y = 40; 
if _N_ = 5 then y = 50; 
if _N_ = 6 then y = 60; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the best way to check it out would be to generate a test dataset temp:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
do x=1 to 10;
output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and run the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 10:28:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860624#M38024</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-02-24T10:28:02Z</dc:date>
    </item>
    <item>
      <title>Re: UNDERSTANDING OF THE CODE</title>
      <link>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860653#M38025</link>
      <description>&lt;P&gt;Great explanation from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I add that if you see macro code, and don't understand what it is doing, you can turn on macro debugging options before you run the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So now if you run the code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymacro(k);
data want;
set temp;
%do i = 1 %to &amp;amp;k;
if _N_ = &amp;amp;i then y = %eval(&amp;amp;i.* 10);
%end;
run;
%mend;
%mymacro(6)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then you see this in the log, this is the code generated by the macro&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;MPRINT(MYMACRO):   data want;
MPRINT(MYMACRO):   set temp;
MPRINT(MYMACRO):   if _N_ = 1 then y = 10;
MPRINT(MYMACRO):   if _N_ = 2 then y = 20;
MPRINT(MYMACRO):   if _N_ = 3 then y = 30;
MPRINT(MYMACRO):   if _N_ = 4 then y = 40;
MPRINT(MYMACRO):   if _N_ = 5 then y = 50;
MPRINT(MYMACRO):   if _N_ = 6 then y = 60;
MPRINT(MYMACRO):   run;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So this is yet another way to see what macro code is doing.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 12:54:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860653#M38025</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-02-24T12:54:34Z</dc:date>
    </item>
    <item>
      <title>Re: UNDERSTANDING OF THE CODE</title>
      <link>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860779#M38027</link>
      <description>&lt;P&gt;Also it is worth pointing out that this code is overly complicated. You can easily get the same result without a macro at all:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let k = 6;

data want;
set temp;
if _N_ &amp;lt;= &amp;amp;k then y = _N_* 10;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 22:13:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860779#M38027</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2023-02-24T22:13:41Z</dc:date>
    </item>
    <item>
      <title>Re: UNDERSTANDING OF THE CODE</title>
      <link>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860817#M38028</link>
      <description>&lt;P&gt;This code is a textbook example for the abuse of the macro language. It illustrates the inefficiency of &lt;EM&gt;repeatedly creating code&lt;/EM&gt; vs. using &lt;EM&gt;repeating code&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;Let's make a small dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
do x = 1 to 10000;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt; 69         data have;
 70         do x = 1 to 10000;
 71           output;
 72         end;
 73         run;
 
 NOTE: The data set WORK.HAVE has 10000 observations and 1 variables.
 NOTE:  Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit):
       real time           0.00 seconds
       user cpu time       0.00 seconds
       system cpu time     0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;As you can see, this step runs in almost no time.&lt;/P&gt;
&lt;P&gt;Now, let's apply your macro to half of the dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mymacro(k);
data want;
set have;
%do i = 1 %to &amp;amp;k.;
if _n_ = &amp;amp;i. then y = %eval(&amp;amp;i. * 10);
%end;
run;
%mend;
%mymacro(5000)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt; 69         %macro mymacro(k);
 70         data want;
 71         set have;
 72         %do i = 1 %to &amp;amp;k.;
 73         if _n_ = &amp;amp;i. then y = %eval(&amp;amp;i. * 10);
 74         %end;
 75         run;
 76         %mend;
 77         %mymacro(5000)
 
 NOTE: There were 10000 observations read from the data set WORK.HAVE.
 NOTE: The data set WORK.WANT has 10000 observations and 2 variables.
 NOTE:  Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit):
       real time           0.79 seconds
       user cpu time       0.78 seconds
       system cpu time     0.01 seconds
&lt;/PRE&gt;
&lt;P&gt;Now, use the simple code which uses the internal loop of the data step itself and only data step language:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let k = 5000;

data want2;
set have;
if _n_ le &amp;amp;k. then y = _n_ * 10;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt; 69         %let k = 5000;
 70         
 71         data want2;
 72         set have;
 73         if _n_ le &amp;amp;k. then y = _n_ * 10;
 74         run;
 
 NOTE: There were 10000 observations read from the data set WORK.HAVE.
 NOTE: The data set WORK.WANT2 has 10000 observations and 2 variables.
 NOTE:  Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit):
       real time           0.00 seconds
       user cpu time       0.00 seconds
       system cpu time     0.00 seconds
&lt;/PRE&gt;
&lt;P&gt;Wow! While the use of data step language runs in next to no time (like the creation of the dataset itself), the macro loop consumes a whopping 0.8 seconds! Why is this?&lt;/P&gt;
&lt;P&gt;The macro loop &lt;EM&gt;creates&lt;/EM&gt; 5000 IF statements and runs them 10000 times, leading to the execution of &lt;U&gt;50 million&lt;/U&gt; logical evaluations during the data step. Such evaluations are the most time-consuming operations for any CPU (except in quantum computers). And you also must consider that compiling 5000 lines of code itself takes much longer than compiling a single line.&lt;/P&gt;
&lt;P&gt;The data step solution runs just 10000 IFs, so it stays well within the limits set by simply reading and writing data from/to storage.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To me, this piece of code is an example for what the premature teaching of macro coding to SAS newbies causes. Before they have even an inkling of what the SAS language (procedures and data steps) is really capable of, the &lt;EM&gt;seemingly&lt;/EM&gt; simple tool of the macro language misguides them, leading to code which is hard to understand and is inefficient on top.&lt;/P&gt;
&lt;P&gt;Heed Maxim 11. First exploit the capabilities of the Base SAS language and tools, and resort to macro programming only once this is exhausted. The most important skill of a macro programmer is to know when to not use the macro language.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS just to make sure that the codes result in the same outcome, run this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc compare base=want compare=want2;
var x y;
with x y;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 25 Feb 2023 08:58:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860817#M38028</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-02-25T08:58:12Z</dc:date>
    </item>
    <item>
      <title>Re: UNDERSTANDING OF THE CODE</title>
      <link>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860840#M38029</link>
      <description>&lt;P&gt;Kurt ( &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;),&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your comment, you gave a beautiful explanation to my oversimplified comment about the original code, which was:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;First of of all, this code is just an &lt;STRONG&gt;ugly&lt;/STRONG&gt; version of:&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;data want; 
  set temp; 
  if _N_ &amp;lt;= 6 then y = _N_*10; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 25 Feb 2023 14:07:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860840#M38029</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-02-25T14:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: UNDERSTANDING OF THE CODE</title>
      <link>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860869#M38030</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; It's also a classic example of someone else's code leading a SAS beginner astray...when maybe that someone else should know better.&lt;/P&gt;</description>
      <pubDate>Sat, 25 Feb 2023 20:46:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860869#M38030</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2023-02-25T20:46:12Z</dc:date>
    </item>
    <item>
      <title>Re: UNDERSTANDING OF THE CODE</title>
      <link>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860876#M38031</link>
      <description>&lt;P&gt;I could wish that some of my inherited code was only this poor...&lt;/P&gt;</description>
      <pubDate>Sun, 26 Feb 2023 00:09:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/UNDERSTANDING-OF-THE-CODE/m-p/860876#M38031</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-26T00:09:03Z</dc:date>
    </item>
  </channel>
</rss>

