<?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 learn macros ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/569731#M160593</link>
    <description>&lt;P&gt;The first (and really essential, important and absolutely necessary) step in good macro coding:&amp;nbsp;achieve proficiency in coding without using macros.&lt;/P&gt;
&lt;P&gt;The macro preprocessor is a code generator, and you'll only be able to use it properly when you have very good knowledge of the code you will be creating with the macro.&lt;/P&gt;
&lt;P&gt;And very often you will find that proper knowledge of the non-macro tools leads to the gist of Maxim 11: A Macro is not Needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;90% of the problems with macros we encounter here on the communities comes from premature tinkering with macros and trying to solve data issues with the wrong tool.&lt;/P&gt;</description>
    <pubDate>Fri, 28 Jun 2019 09:46:15 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-06-28T09:46:15Z</dc:date>
    <item>
      <title>how to learn macros ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/569715#M160588</link>
      <description>&lt;P&gt;Hi sas support community&amp;nbsp; i am very poor in sas macros can anyone sugest how improve macro coding?&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jun 2019 07:29:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/569715#M160588</guid>
      <dc:creator>KHKrishna</dc:creator>
      <dc:date>2019-06-28T07:29:46Z</dc:date>
    </item>
    <item>
      <title>Re: how to learn macros ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/569717#M160589</link>
      <description>&lt;P&gt;Hi and welcome to the SAS Communities &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lots of learning material out there. But the best is probably the Documentation itself:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=p1ccprwibo8gvqn1q6zinvjlzuut.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;Getting Started with the Macro Facility&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jun 2019 07:31:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/569717#M160589</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-06-28T07:31:55Z</dc:date>
    </item>
    <item>
      <title>Re: how to learn macros ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/569731#M160593</link>
      <description>&lt;P&gt;The first (and really essential, important and absolutely necessary) step in good macro coding:&amp;nbsp;achieve proficiency in coding without using macros.&lt;/P&gt;
&lt;P&gt;The macro preprocessor is a code generator, and you'll only be able to use it properly when you have very good knowledge of the code you will be creating with the macro.&lt;/P&gt;
&lt;P&gt;And very often you will find that proper knowledge of the non-macro tools leads to the gist of Maxim 11: A Macro is not Needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;90% of the problems with macros we encounter here on the communities comes from premature tinkering with macros and trying to solve data issues with the wrong tool.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jun 2019 09:46:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/569731#M160593</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-28T09:46:15Z</dc:date>
    </item>
    <item>
      <title>Re: how to learn macros ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/569889#M160648</link>
      <description>&lt;P&gt;As others have indicated, macros should be a last resort.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming you already know the following well:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;BY group processing&lt;/LI&gt;
&lt;LI&gt;data step processing (with output)&lt;/LI&gt;
&lt;LI&gt;arrays&lt;/LI&gt;
&lt;LI&gt;procs such as : freq, means (class, ways and types statements are powerful), freq, transpose, tabulate and report&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Some good resources:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro appendix, in the documentation has a set of good examples:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My short tutorial on creating a macro:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One general premise of writing a macro - first make sure you have working code that solves the situation for a base case and then generalize it. If you skip this step, you run the risk of spending hours debugging.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jun 2019 16:14:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/569889#M160648</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-28T16:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: how to learn macros ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/572761#M161657</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can execute the following program and read the log to have an illustration of&lt;/P&gt;
&lt;P&gt;the main concepts of SAS macro programming.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Macro quick tutorial */

/* Note: SAS macros are used to generate SAS code and should not be used for processing data */

options mprint mlogic symbolgen; /* Add useful informations in the log */

%let mv=10; /* a macro variable is initialized with %let */

/* &amp;amp;&amp;lt;macrovariable&amp;gt;. is replaced at compile time by its value */

%put &amp;amp;mv.; /* 10 displayed in the log */

data ds&amp;amp;mv.; /* data ds10; */
    set sashelp.class;
run;

%let mv2=&amp;amp;mv.+1; /* 10+1 not 11  =&amp;gt; only text substition */

%put &amp;amp;mv2.; /* 10+1 displayed in the log */

data ds&amp;amp;mv2.; /* data ds10+1; =&amp;gt; ERROR */
    set sashelp.class;
run;

%let mv3=%eval(&amp;amp;mv.+1); /* 11 */

%put &amp;amp;mv3.;

/* A macro variable can hold the name of another macro variable */
%let x=5;
%let mvname=x;

/* During the resolution, two consecutive ampersands are resolved in a single ampersand */
%put &amp;amp;mvname.;   /* x */
%put &amp;amp;&amp;amp;mvname.;  /* x ((&amp;amp;&amp;amp;)mvname. =&amp;gt; &amp;amp;mvname. =&amp;gt; x) */
%put &amp;amp;&amp;amp;&amp;amp;mvname.; /* 5 ((&amp;amp;&amp;amp;)(&amp;amp;mvname.) =&amp;gt; &amp;amp;x. =&amp;gt; 5)*/

data _NULL_;
    set sashelp.class;
    /* Creates a macrovariable from a dataset column    */
    /* The instruction is executed for each row of data */
    call symputx("name", name);

    %put &amp;amp;name.; /* &amp;amp;name. will only be available at the end of the datastep   */
                 /* =&amp;gt; WARNING if the macrovariable does not exist previous to */
                 /* the data step                                              */
                 /* otherwise, its value before the data step.                 */
run;

%put &amp;amp;name.; /* William (last observation of sashelp.class) */

data _NULL_;
    set sashelp.class;
    call symputx(cats("name",_N_), name);
run;

%put &amp;amp;name1. &amp;amp;name2. &amp;amp;name3.; /* Alice Barbara Carol */

/* Affectation of macrovariables to dataset columns */
data macro;
    length will name $32.;

    will=symget("name");             /* William */
    will2="&amp;amp;name.";                  /* William */
    will3='&amp;amp;name.';                  /* &amp;amp;name. =&amp;gt; no resolution with single quotes */
    x=symgetn("x");                  /* 5 */

    do _N_=1 to countw("&amp;amp;names.");
        name=symget(cats("name",_N_));   /* Alice, Barbara, Carol, ... William */
        output;
    end;
run;

/* Create macrovariables from dataset columns with proc sql */
proc sql noprint;
    SELECT name, name
    INTO :name, :names SEPARATED BY ' '
    FROM sashelp.class;
quit;

%put &amp;amp;name.; /* Alice */
%put &amp;amp;names.; /* Alice Barbara Carol ... Thomas William */

/* Macro functions are necessary for conditional branching, loops   */
/* They can be used for repetitive tasks to avoid replicating large */
/* chunks of code.                                                  */
%macro f(pos, kw=default, kw2=);
    /* pos : positional argument                                        */
    /* kw, kw2 : keyword parameters                                     */
    /* positional parameters must apperar before keyword parameters     */

    %if &amp;amp;pos.=1 %then %do;
        %put OK;
    %end;
    %else %do;
        %put KO;
    %end;

    %put &amp;amp;kw. &amp;amp;kw2;

%mend; /* end of the macro function definition */

%f(1)            /* OK default       */
%f(2, kw2=hello) /* KO default hello */
%f(2, kw=foo)    /* KO foo */
%f(kw=bar)       /* KO bar */
%f(kw=bar,pos=0) /* KO bar =&amp;gt; positional parameters can also be called by name */

/* Usage of macro loops */
%macro f2(ds, out);

    %do i=1 %to 5;
        data &amp;amp;out.&amp;amp;i.;
            row=&amp;amp;i.;
            set &amp;amp;ds. point=row;
            output; stop;
        run;

        /* Loops can be used to create indexed macrovariables */
        %let x&amp;amp;i.=&amp;amp;i.; /* x1=1, x2=2, ..., x5=5 */

        %put &amp;amp;&amp;amp;x&amp;amp;i.; /* 1, 2, 3, 4, 5 ((&amp;amp;&amp;amp;)(x&amp;amp;i). =&amp;gt; &amp;amp;x1. =&amp;gt; 1 */
    %end;

%mend;

%f2(sashelp.class, student) /* datasets student1 (Alice), student2 (Barbara) */

/* Scope */

%let j=1; /* defined outside macro =&amp;gt; global scope */

%macro a;

    %do i=1 %to 5;
        %put &amp;amp;i.;
    %end;

%mend;

%macro main;

    /* The same counter name (i) is used in macro main and macro a */
    %do i=1 %to 2;
        %a;
    %end;

    %let j=2; /* Modifies the global macro variable j */

%mend;

%main /* 1 2 3 4 5 (the value of i in macro a replaced the one computed in macro main) */
      /* =&amp;gt; Only one iteration of the loop in main                                     */

%put &amp;amp;j.; /* 2 (modified by the macro) */

%macro b;

    %local i; /* i is now a macrovariable specific to the macro b */ 

    %do i=1 %to 5;
        %put &amp;amp;i.;
    %end;

%mend;

%macro main2;

    %local j;

    %do i=1 %to 2;
        %b;
    %end;

    %let j=5;

%mend;

%main2 /* 1 2 3 4 5 1 2 3 4 5 */

%put &amp;amp;j.; /* 2 (the global variable value has not been replaced) */
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Edit : Added &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; example of macro call with names positional argument.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2019 14:44:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/572761#M161657</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-07-11T14:44:02Z</dc:date>
    </item>
    <item>
      <title>Re: how to learn macros ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/572764#M161658</link>
      <description>&lt;P&gt;I would add an example in your block of example macro calls where you call the positional parameter by name.&amp;nbsp; &amp;nbsp;That is a very nice usability feature of SAS macro calls that other languages often lack.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%f(kw=bar,pos=0)       /* KO bar */
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Jul 2019 14:37:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/572764#M161658</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-11T14:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: how to learn macros ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/572767#M161659</link>
      <description>Done, thanks.</description>
      <pubDate>Thu, 11 Jul 2019 14:44:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/572767#M161659</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-07-11T14:44:28Z</dc:date>
    </item>
    <item>
      <title>Re: how to learn macros ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/573184#M161782</link>
      <description>Keep it simple.  The first step should be getting one simple macro working and then expanding it from there.  &lt;BR /&gt;&lt;BR /&gt;Start with something that has no variables at all and then add one...keep building from there, growing your comfort as you go.</description>
      <pubDate>Fri, 12 Jul 2019 18:42:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/573184#M161782</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2019-07-12T18:42:27Z</dc:date>
    </item>
    <item>
      <title>Re: how to learn macros ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/576143#M163045</link>
      <description>&lt;P&gt;My fun answer of the day:&amp;nbsp; Find Ron Fehd, spend a whole week with him, and you, too will become a Macro Expert.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img id="smileyvery-happy" class="emoticon emoticon-smileyvery-happy" src="https://communities.sas.com/i/smilies/16x16_smiley-very-happy.png" alt="Smiley Very Happy" title="Smiley Very Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2019 13:05:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-learn-macros/m-p/576143#M163045</guid>
      <dc:creator>PabloNogueras</dc:creator>
      <dc:date>2019-07-24T13:05:53Z</dc:date>
    </item>
  </channel>
</rss>

