<?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 Doing a do loop within SAS Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Doing-a-do-loop-within-SAS-Macro/m-p/229204#M267972</link>
    <description>&lt;P&gt;I have the following code:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;%macro initial (first=, second=, third=, fourth=, final=); 
   data &amp;amp;first;
     set wtnodup.&amp;amp;first;
         DATE1 = INPUT(PUT(Date,8.),YYMMDD8.);
         format DATE1 monyy7.;
 RUN;

proc freq data=&amp;amp;first order= freq;                                                                                                                              
   tables date1*jobboardid / list out=&amp;amp;second (drop = percent rename=   
                                              (Count=CountNew));
 run;

 data &amp;amp;third;
  set &amp;amp;second (firstobs=2);
    if countnew le 49 then delete; 
 run;
proc sort data = &amp;amp;third;
   by jobboardid Date1;    
run;
data &amp;amp;fourth (keep = countnew oldcountnew Date1 rate from till jobboardid 
                                                                    rate);
    set &amp;amp;third;
        by jobboardid Date1;
        format From Till monyy7.;
        from = lag12(Date1);
        oldcountnew = lag12(countnew);

        if lag12(jobboardid) EQ jobboardid and
        INTCK('month', from, Date1) EQ 12 then do;
        till = Date1;
        rate =  ((countnew/oldcountnew)-1)*100;
       output;
    end;
run;
proc sort data = &amp;amp;fourth;
   by Date1 rate;
proc means data=&amp;amp;fourth noprint;
   by Date1;
output out=Result.&amp;amp;final median(rate)=medianRate;
run; 

%mend initial;
%initial (first = Alabama, second = AlabamaOne, third =AlabamaTwo, 
          fourth = AlabamaThree, final=AL_10);
%initial (first = Alaska, second = AlaskaOne, third =AlaskaTwo, 
          fourth = AlaskaThree, final=AK_10);
%initial (first = Arizona, second = ArizonaOne, third =ArizonaTwo, 
          fourth = ArizonaThree, final=AZ);
%initial (first = Arkansas, second = ArkansasOne, third =ArkansasTwo, 
          fourth= ArkansasThree, final=AR_10);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What I am trying to do is that in the part that puts the condition:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;if countnew &amp;lt; 10 then delete; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to create a sort of do-loop that would go through the entire steps within the macro once for when countnew &amp;lt;10 and then creates a seperate dataset for when countnew &amp;lt;10 for each state....&lt;BR /&gt;And then does the same thing for when it's 20, 30, 40, until 70. So at the end I will have 7 differenct datasets for each state.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;What is the best way to go about doing this?&lt;/P&gt;</description>
    <pubDate>Thu, 08 Oct 2015 23:11:52 GMT</pubDate>
    <dc:creator>mustafghan</dc:creator>
    <dc:date>2015-10-08T23:11:52Z</dc:date>
    <item>
      <title>Doing a do loop within SAS Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Doing-a-do-loop-within-SAS-Macro/m-p/229204#M267972</link>
      <description>&lt;P&gt;I have the following code:&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;%macro initial (first=, second=, third=, fourth=, final=); 
   data &amp;amp;first;
     set wtnodup.&amp;amp;first;
         DATE1 = INPUT(PUT(Date,8.),YYMMDD8.);
         format DATE1 monyy7.;
 RUN;

proc freq data=&amp;amp;first order= freq;                                                                                                                              
   tables date1*jobboardid / list out=&amp;amp;second (drop = percent rename=   
                                              (Count=CountNew));
 run;

 data &amp;amp;third;
  set &amp;amp;second (firstobs=2);
    if countnew le 49 then delete; 
 run;
proc sort data = &amp;amp;third;
   by jobboardid Date1;    
run;
data &amp;amp;fourth (keep = countnew oldcountnew Date1 rate from till jobboardid 
                                                                    rate);
    set &amp;amp;third;
        by jobboardid Date1;
        format From Till monyy7.;
        from = lag12(Date1);
        oldcountnew = lag12(countnew);

        if lag12(jobboardid) EQ jobboardid and
        INTCK('month', from, Date1) EQ 12 then do;
        till = Date1;
        rate =  ((countnew/oldcountnew)-1)*100;
       output;
    end;
run;
proc sort data = &amp;amp;fourth;
   by Date1 rate;
proc means data=&amp;amp;fourth noprint;
   by Date1;
output out=Result.&amp;amp;final median(rate)=medianRate;
run; 

%mend initial;
%initial (first = Alabama, second = AlabamaOne, third =AlabamaTwo, 
          fourth = AlabamaThree, final=AL_10);
%initial (first = Alaska, second = AlaskaOne, third =AlaskaTwo, 
          fourth = AlaskaThree, final=AK_10);
%initial (first = Arizona, second = ArizonaOne, third =ArizonaTwo, 
          fourth = ArizonaThree, final=AZ);
%initial (first = Arkansas, second = ArkansasOne, third =ArkansasTwo, 
          fourth= ArkansasThree, final=AR_10);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What I am trying to do is that in the part that puts the condition:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;if countnew &amp;lt; 10 then delete; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to create a sort of do-loop that would go through the entire steps within the macro once for when countnew &amp;lt;10 and then creates a seperate dataset for when countnew &amp;lt;10 for each state....&lt;BR /&gt;And then does the same thing for when it's 20, 30, 40, until 70. So at the end I will have 7 differenct datasets for each state.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;What is the best way to go about doing this?&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2015 23:11:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Doing-a-do-loop-within-SAS-Macro/m-p/229204#M267972</guid>
      <dc:creator>mustafghan</dc:creator>
      <dc:date>2015-10-08T23:11:52Z</dc:date>
    </item>
    <item>
      <title>Re: Doing a do loop within SAS Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Doing-a-do-loop-within-SAS-Macro/m-p/229231#M267973</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you post example data of what you have - in the form of a datastep, and what you want out at the end. &amp;nbsp;I am not seeing anything there that can't be done in straight datastep code.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Oct 2015 09:30:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Doing-a-do-loop-within-SAS-Macro/m-p/229231#M267973</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-10-09T09:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: Doing a do loop within SAS Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Doing-a-do-loop-within-SAS-Macro/m-p/229475#M267974</link>
      <description>Is there a reason for keeping data in separate data sets? By having all data in one place usually makes life easier, and probably make macro logic obsolete.</description>
      <pubDate>Sun, 11 Oct 2015 13:31:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Doing-a-do-loop-within-SAS-Macro/m-p/229475#M267974</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2015-10-11T13:31:34Z</dc:date>
    </item>
    <item>
      <title>Re: Doing a do loop within SAS Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Doing-a-do-loop-within-SAS-Macro/m-p/229604#M267975</link>
      <description>&lt;P&gt;I don't understand where you want to output the multiple datasets, but what's for sure is that you have way more&amp;nbsp;steps than needed.&lt;/P&gt;&lt;P&gt;Try something like this, which will be more efficient and easier to follow:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        
proc freq data=wtnodup.&amp;amp;first.;                                                                                                                              
   tables JOBBOARDID*DATE / list out=&amp;amp;first.  (drop  = PERCENT 
                                               rename=(COUNT=COUNTNEW)
                                               where =(COUNTNEW gt 49));
 run;

data &amp;amp;second. (keep = COUNTNEW OLDCOUNTNEW DATE1 RATE FROM TILL JOBBOARDID RATE);
  set &amp;amp;first.;
  by JOBBOARDID DATE;
  format DATE1 FROM TILL monyy7.;

  DATE1       = input(cats(DATE),yymmdd8.);
  FROM        = lag12(DATE1);
  OLDCOUNTNEW = lag12(COUNTNEW);

  if   lag12(JOBBOARDID) eq JOBBOARDID 
   and intck('month', FROM, DATE1) eq 12 then do;
    TILL = DATE1;
    RATE = ((COUNTNEW/OLDCOUNTNEW)-1)*100;
    output;
  end;
run;

proc means data=&amp;amp;second. noprint nway;
  class DATE1;
  output out=RESULT.&amp;amp;final. median(RATE)=MEDIANRATE;
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many more optimisation tips in &lt;A href="http://www.amazon.com/High-Performance-SAS-Coding-Christian-Graffeuille/dp/1512397490" target="_blank"&gt;http://www.amazon.com/High-Performance-SAS-Coding-Christian-Graffeuille/dp/1512397490&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Oct 2015 22:06:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Doing-a-do-loop-within-SAS-Macro/m-p/229604#M267975</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2015-10-12T22:06:14Z</dc:date>
    </item>
  </channel>
</rss>

