<?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: Call a Macro With Each Observation In a Dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/475369#M285982</link>
    <description>&lt;P&gt;Call execute is probably the simplest way.&amp;nbsp; What I would suggest however is to check what you want to do.&amp;nbsp; Sometimes, not always, it is possible to re-write the code slightly in order to use by grouping rather than go through the whole macro creation and call with each set of parameters.&amp;nbsp; For instance, if the first datastep or proc in your macro filters out data based on the parameters then I can pretty much guarentee you that replacing the macro and setting the steps to us by group processing will be both simpler coding, faster operation, and less storage.&lt;/P&gt;</description>
    <pubDate>Wed, 04 Jul 2018 07:41:56 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-07-04T07:41:56Z</dc:date>
    <item>
      <title>Call a Macro With Each Observation In a Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/475339#M285977</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; I've got a problem where I need to loop through observation in a dataset&amp;nbsp;and call a macro with each observation.&lt;/P&gt;&lt;P&gt;Here is an example dataset:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.schedule; 
input id 8. nm $20. senddays $14.;
datalines4;
2013312	México              3
2010941 Ghana               5
2013320 England             1
2013398 Argentina           5
2013363 Philippines         2
2013789 Spain               3
;;;;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Each set of&amp;nbsp;id, nm and senddays are parameters in a macro call.&amp;nbsp; For example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro doStuff(id=,nm=,sendday=);
/* calculate stuff, run procedures, etc */
%mend doStuff;

/* pseudocode that I need to achieve*/&lt;BR /&gt;&lt;BR /&gt;%macro main;
%do for each observation i in work.schedule;
    %doStuff(i.id,i.nm,i.sendday);
end;&lt;BR /&gt;&lt;BR /&gt;%mend main;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;%main&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;How do I do this in SAS?&amp;nbsp; Does it have to be done in a %do loop inside a macro or is there another way?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jul 2018 23:40:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/475339#M285977</guid>
      <dc:creator>yus03590</dc:creator>
      <dc:date>2018-07-03T23:40:09Z</dc:date>
    </item>
    <item>
      <title>Re: Call a Macro With Each Observation In a Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/475342#M285978</link>
      <description>&lt;P&gt;You can create macro variables inside a data step using CALL SYMPUTX, and then after assigning the value of the country name to the maacro variable, you can run your macro %dostuff. UNTESTED CODE, assumes you have computed or know the number of observations in your data set, and this is the value of macro variable &amp;amp;nobs&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro do_all;

%do i=1 %to &amp;amp;nobs;
    Data _null_;
        Set have(firstobs=&amp;amp;i obs=&amp;amp;i);
        call symputx('nm',nm);
        Call symputx('id',id);
        Call symputx('senddays',senddays);
    run;
    %dostuff(id=&amp;amp;id,nm=&amp;amp;nm,sendday=&amp;amp;senddays)
%end;

%mend;
%do_all&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you can do this via CALL EXECUTE, but I'll let someone else describe that code.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jul 2018 00:40:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/475342#M285978</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-07-04T00:40:34Z</dc:date>
    </item>
    <item>
      <title>Re: Call a Macro With Each Observation In a Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/475345#M285979</link>
      <description>&lt;P&gt;CALL EXECUTE is designed for this and pretty straightforward. You need to make a string that has the values you need and basically looks like your macro call,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%doStuff(id=201312 , nm=Mexico, sendday=3);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can use CATT() to create the string and then use CALL EXECUTE to execute the string. I've commented it out below, but you can uncomment and run it. Additionally, if you don't need the intermediary data set change the run_macros to _null_.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data run_macros;
set schedule;

str = catt('%doStuff(id=', 
            id, 
            ', nm=',
            nm,
            ', sendday=', 
            senddays, 
            ');');
*call execute (str);

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/181365"&gt;@yus03590&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; I've got a problem where I need to loop through observation in a dataset&amp;nbsp;and call a macro with each observation.&lt;/P&gt;
&lt;P&gt;Here is an example dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.schedule; 
input id 8. nm $20. senddays $14.;
datalines4;
2013312	México              3
2010941 Ghana               5
2013320 England             1
2013398 Argentina           5
2013363 Philippines         2
2013789 Spain               3
;;;;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Each set of&amp;nbsp;id, nm and senddays are parameters in a macro call.&amp;nbsp; For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro doStuff(id=,nm=,sendday=);
/* calculate stuff, run procedures, etc */
%mend doStuff;

/* pseudocode that I need to achieve*/&lt;BR /&gt;&lt;BR /&gt;%macro main;
%do for each observation i in work.schedule;
    %doStuff(i.id,i.nm,i.sendday);
end;&lt;BR /&gt;&lt;BR /&gt;%mend main;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;%main&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How do I do this in SAS?&amp;nbsp; Does it have to be done in a %do loop inside a macro or is there another way?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jul 2018 01:29:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/475345#M285979</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-07-04T01:29:50Z</dc:date>
    </item>
    <item>
      <title>Re: Call a Macro With Each Observation In a Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/475350#M285980</link>
      <description>&lt;P&gt;That is a good example of where using a PUT statement to write the code can be very easy.&lt;/P&gt;
&lt;P&gt;Especially if you fix the variable names in your metadata table to match the parameter names used by your macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.schedule; 
input id 8. nm $20. sendday $14.;
datalines4;
2013312	México              3
2010941 Ghana               5
2013320 England             1
2013398 Argentina           5
2013363 Philippines         2
2013789 Spain               3
;;;;
run;

filename code temp;
data _null_;
  set work.schedule ;
  file code ;
  put '%doStuff(' id= ',' nm= ',' sendday= ');';
run;
%include code / source2 ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Jul 2018 02:18:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/475350#M285980</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-04T02:18:02Z</dc:date>
    </item>
    <item>
      <title>Re: Call a Macro With Each Observation In a Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/475362#M285981</link>
      <description>&lt;P&gt;Depending on the SAS version you have, you could use the function dosubl in an data-step to execute a macro.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jul 2018 06:48:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/475362#M285981</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-07-04T06:48:04Z</dc:date>
    </item>
    <item>
      <title>Re: Call a Macro With Each Observation In a Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/475369#M285982</link>
      <description>&lt;P&gt;Call execute is probably the simplest way.&amp;nbsp; What I would suggest however is to check what you want to do.&amp;nbsp; Sometimes, not always, it is possible to re-write the code slightly in order to use by grouping rather than go through the whole macro creation and call with each set of parameters.&amp;nbsp; For instance, if the first datastep or proc in your macro filters out data based on the parameters then I can pretty much guarentee you that replacing the macro and setting the steps to us by group processing will be both simpler coding, faster operation, and less storage.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jul 2018 07:41:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/475369#M285982</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-07-04T07:41:56Z</dc:date>
    </item>
    <item>
      <title>Re: Call a Macro With Each Observation In a Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/783806#M285983</link>
      <description>thanks for this, very helpful!</description>
      <pubDate>Fri, 03 Dec 2021 00:17:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Call-a-Macro-With-Each-Observation-In-a-Dataset/m-p/783806#M285983</guid>
      <dc:creator>cameronmuir</dc:creator>
      <dc:date>2021-12-03T00:17:55Z</dc:date>
    </item>
  </channel>
</rss>

