<?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 Using DO and END outside of a datastep?? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-DO-and-END-outside-of-a-datastep/m-p/75140#M16175</link>
    <description>I'm trying to run a piece of code X number of times returning a separate dataset for the last X months.  Can I use do, to and end outside of the datastep, but inside a macro?  It doesn't seem to work.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%macro test(period);&lt;BR /&gt;
&lt;BR /&gt;
%do i=1 %to &amp;amp;period;&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
call symput ('position', i);&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%let posdate=INTnx('MONTH',today(),-&amp;amp;position);&lt;BR /&gt;
&lt;BR /&gt;
data pos&amp;amp;position;&lt;BR /&gt;
set inputdata;&lt;BR /&gt;
where timeid=&amp;amp;posdate;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=pos&amp;amp;position;&lt;BR /&gt;
by accountid;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
%test(12);</description>
    <pubDate>Mon, 23 Feb 2009 08:21:05 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2009-02-23T08:21:05Z</dc:date>
    <item>
      <title>Using DO and END outside of a datastep??</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-DO-and-END-outside-of-a-datastep/m-p/75140#M16175</link>
      <description>I'm trying to run a piece of code X number of times returning a separate dataset for the last X months.  Can I use do, to and end outside of the datastep, but inside a macro?  It doesn't seem to work.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%macro test(period);&lt;BR /&gt;
&lt;BR /&gt;
%do i=1 %to &amp;amp;period;&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
call symput ('position', i);&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%let posdate=INTnx('MONTH',today(),-&amp;amp;position);&lt;BR /&gt;
&lt;BR /&gt;
data pos&amp;amp;position;&lt;BR /&gt;
set inputdata;&lt;BR /&gt;
where timeid=&amp;amp;posdate;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=pos&amp;amp;position;&lt;BR /&gt;
by accountid;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
%test(12);</description>
      <pubDate>Mon, 23 Feb 2009 08:21:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-DO-and-END-outside-of-a-datastep/m-p/75140#M16175</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-02-23T08:21:05Z</dc:date>
    </item>
    <item>
      <title>Re: Using DO and END outside of a datastep??</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-DO-and-END-outside-of-a-datastep/m-p/75141#M16176</link>
      <description>cstayner,&lt;BR /&gt;
&lt;BR /&gt;
You're nearly there. I'd make a few changes:&lt;BR /&gt;
&lt;BR /&gt;
Your null data step is the right idea, but I'd avoid adding a variable that doubles up on i, you could get the data step doing some work, and move the posdate calculation here:&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
call symput ('posdate', INTnx('MONTH',today(),-&amp;amp;i));&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
This would be quite a bit better - the way you have written the posdate assignment will have problems because the intnx() and today() functions are not macro functions. The alternative would be:&lt;BR /&gt;
&lt;BR /&gt;
%let posdate=%sysfunc(INTnx('MONTH',%sysfunc(today()),-&amp;amp;position));&lt;BR /&gt;
&lt;BR /&gt;
In this case I'd be inclined, though, to restructure the code, see below:&lt;BR /&gt;
&lt;BR /&gt;
data have(keep=account_id time_id);&lt;BR /&gt;
   do t = 0 to 11;&lt;BR /&gt;
      do account_id = 12 to 1 by -1;   &lt;BR /&gt;
         time_id = intnx('month', '01Jan2008'd, t);&lt;BR /&gt;
         output;&lt;BR /&gt;
      end;&lt;BR /&gt;
   end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%macro sortseg(months=12);&lt;BR /&gt;
&lt;BR /&gt;
proc sort&lt;BR /&gt;
   data = have&lt;BR /&gt;
   ;&lt;BR /&gt;
   by account_id&lt;BR /&gt;
   ;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/* Create the macro variables we want, one set for months, one for datasets */&lt;BR /&gt;
data _null_;&lt;BR /&gt;
   do m = 1 to &amp;amp;months;&lt;BR /&gt;
      call symput('month_'||compress(m), compress(intnx('month', '01Jan2009'd, -m)));&lt;BR /&gt;
      call symput('ds_out_'||compress(m), 'want_'||compress(m));;&lt;BR /&gt;
   end;&lt;BR /&gt;
run;&lt;BR /&gt;
/* Specify multiple output datasets */&lt;BR /&gt;
data &lt;BR /&gt;
   %do month_loop = 1 %to &amp;amp;months;&lt;BR /&gt;
      &amp;amp;&amp;amp;ds_out_&amp;amp;month_loop &lt;BR /&gt;
   %end;&lt;BR /&gt;
   ;&lt;BR /&gt;
   set have;&lt;BR /&gt;
   /* Use if, rather than where, to conditionally output */&lt;BR /&gt;
   %do month_loop = 1 %to &amp;amp;months;&lt;BR /&gt;
      if time_id = &amp;amp;&amp;amp;month_&amp;amp;month_loop then output &amp;amp;&amp;amp;ds_out_&amp;amp;month_loop;&lt;BR /&gt;
   %end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
%sortseg(months=12)&lt;BR /&gt;
&lt;BR /&gt;
The macro code is writing the SAS code, rather than executing the process, and this is one of the key things to remember about how macro and SAS code interact.&lt;BR /&gt;
&lt;BR /&gt;
I hope this helps,&lt;BR /&gt;
&lt;BR /&gt;
ProcMe</description>
      <pubDate>Mon, 23 Feb 2009 11:08:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-DO-and-END-outside-of-a-datastep/m-p/75141#M16176</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-02-23T11:08:15Z</dc:date>
    </item>
  </channel>
</rss>

