<?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: Macro to loop through multiple years to process a chunk of codes all together in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883789#M349163</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;Thanks Tom! You were right, I added FY as a BY variable and figured out a code (without doing macro) that works.&lt;/P&gt;
&lt;PRE&gt;PROC SORT DATA=FINAL OUT=SUB1;
	BY FY CASELOAD RC2;
RUN;

DATA SUB1;
	SET SUB1;
	BY FY CASELOAD;
	IF FIRST.RC2 OR FIRST.FY THEN DCOUNTER=0;
	IF FIRST.FY THEN FYCNT=0;
	IF CASELOAD LT 11 AND CASELOAD NE 0 THEN DO;
		DELETE=1;
		DCOUNTER+1;
		FYCNT+1;
	END;
	LAST_DELETE=IFN(FIRST.RC2,.,LAG(DELETE));
	*IF LAST_DELETE AND DCOUNTER LT 2 THEN DELETE=1; 
RUN;

PROC SQL;
	CREATE TABLE WANT1 AS
	SELECT *, SUM(DELETE) AS MAXD 
FROM SUB1
GROUP BY FY
;	
QUIT;

PROC SORT DATA=WANT1; BY FY CASELOAD RC2; RUN;

DATA WANT1; SET WANT1;
	IF MAXD=1 AND LAST_DELETE=1 THEN CASELOAD=CASELOAD*-1;
	KEEP FY RC2 POS CASELOAD;
RUN;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code also works fine!&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Try just adding FY as the FIRST by variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try running this code to do two fiscal years.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=final out=sub1;
  by fy caseload rc2;
  where fy in ('FY16/17' 'FY17/18') ;
run;

data sub1;
  set sub1;
  by fy caseload rc2;
  if first.rc2 then dcounter=0;
  if caseload lt 11 and caseload ne 0 then do;
    delete=1;
    dcounter+1;
  end;
  last_delete=ifn(first.rc2,.,lag(delete));
  if last_delete and dcounter lt 2 then delete=1;
run;

proc sql;
create table want1 as
  select *, max(dcounter) as maxd
  from sub1
  group by fy
;
quit;

data want1; set want1;
  if maxd=1 and delete=1 and last_delete=1 then caseload=caseload*-1;
  keep fy rc2 pos caseload;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If it works then just update the WHERE statement to select&amp;nbsp; all of the years you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  where 'FY16' &amp;lt;=: fy &amp;lt;: 'FY20' ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Try just adding FY as the FIRST by variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try running this code to do two fiscal years.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=final out=sub1;
  by fy caseload rc2;
  where fy in ('FY16/17' 'FY17/18') ;
run;

data sub1;
  set sub1;
  by fy caseload rc2;
  if first.rc2 then dcounter=0;
  if caseload lt 11 and caseload ne 0 then do;
    delete=1;
    dcounter+1;
  end;
  last_delete=ifn(first.rc2,.,lag(delete));
  if last_delete and dcounter lt 2 then delete=1;
run;

proc sql;
create table want1 as
  select *, max(dcounter) as maxd
  from sub1
  group by fy
;
quit;

data want1; set want1;
  if maxd=1 and delete=1 and last_delete=1 then caseload=caseload*-1;
  keep fy rc2 pos caseload;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If it works then just update the WHERE statement to select&amp;nbsp; all of the years you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  where 'FY16' &amp;lt;=: fy &amp;lt;: 'FY20' ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;</description>
    <pubDate>Thu, 06 Jul 2023 18:01:35 GMT</pubDate>
    <dc:creator>kevsma</dc:creator>
    <dc:date>2023-07-06T18:01:35Z</dc:date>
    <item>
      <title>Macro to loop through multiple years to process a chunk of codes all together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883640#M349118</link>
      <description>&lt;P&gt;Hello, I need to set up a macro to process the following few steps all together.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see the following snippets only process data when FY=FY16/17, I have a couple other FYs: FY17/18, FY18/19, FY19/20, FY20/21 and I need to process this same chunk of code for each FY. Is there a way to set up a macro to do it? I don't want to copy and paste a million rows... please help!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;PROC SORT DATA=FINAL(WHERE=(FY='FY16/17')) OUT=SUB1;
	BY CASELOAD RC2;
RUN;

DATA SUB1;
	SET SUB1;
	BY CASELOAD;
	IF FIRST.RC2 THEN DCOUNTER=0;
	IF CASELOAD LT 11 AND CASELOAD NE 0 THEN DO;
		DELETE=1;
		DCOUNTER+1;
	END;
	LAST_DELETE=IFN(FIRST.RC2,.,LAG(DELETE));
	IF LAST_DELETE AND DCOUNTER LT 2 THEN DELETE=1; 
RUN;

PROC SQL;
	CREATE TABLE WANT1 AS
	SELECT *, MAX(DCOUNTER) AS MAXD 
FROM SUB1
;	
QUIT;

DATA WANT1; SET WANT1;
	IF MAXD=1 AND DELETE=1 AND LAST_DELETE=1 THEN CASELOAD=CASELOAD*-1;
	KEEP FY RC2 POS CASELOAD;
RUN;
&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Jul 2023 22:59:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883640#M349118</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2023-07-05T22:59:11Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to loop through multiple years to process a chunk of codes all together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883642#M349119</link>
      <description>&lt;P&gt;The posted code cannot work.&amp;nbsp; You are referencing FIRST. variables that will not exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you did have working code then why not just add FY as the first BY variable?&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2023 23:10:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883642#M349119</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-05T23:10:47Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to loop through multiple years to process a chunk of codes all together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883643#M349120</link>
      <description>&lt;P&gt;When someone says that they "need to set up a macro process", I am very skeptical. I have not gone through you code line by line in detail, but I think what you really need is to create a BY variable which is FY and then let SAS do all the BY processing; no macros needed.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2023 23:16:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883643#M349120</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-07-05T23:16:27Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to loop through multiple years to process a chunk of codes all together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883644#M349121</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp; the code works because FY and RC2 are the two grouping variables in the final dataset, and I did try to add FY as an additional sort by variable, but that will change the way the caseload (numeric) variable was ordered. Essentially, I need to order caseload from lowest to highest and then do suppression for privacy purposes. The suppression has to take place in each FY independently.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2023 23:17:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883644#M349121</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2023-07-05T23:17:47Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to loop through multiple years to process a chunk of codes all together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883645#M349122</link>
      <description>&lt;P&gt;thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp; I did add FY as the BY variable but given the structure of the dataset, caseload is the numeric value that I need to sort from lowest to highest, and two grouping variables: FY and RC2. It looks like once I add FY as BY variable, the order of the values changed as well. Maybe I messed up somewhere when sorting the data, but essentially what I am trying to achieve is to rank caseload from lowest to highest by RC2 in one FY first, then do a bunch of stuff from there and then repeat the same step for other FYs.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2023 23:25:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883645#M349122</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2023-07-05T23:25:13Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to loop through multiple years to process a chunk of codes all together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883646#M349123</link>
      <description>&lt;P&gt;RC2 is NOT included in the BY statement of the data step that is using FIRST.RC2.&amp;nbsp; You cannot use FIRST.RC2 if RC2 is not on the BY statement.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;204 data want;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;205 set sashelp.class;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;206 by name ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;207 if first.age;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;208 run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;NOTE: Variable first.age is uninitialized.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;NOTE: There were 19 observations read from the data set SASHELP.CLASS.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;NOTE: The data set WORK.WANT has 0 observations and 5 variables.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Adding FY before CASELOAD in the BY statement will not change how CASELOAD values sort.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2023 23:28:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883646#M349123</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-05T23:28:09Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to loop through multiple years to process a chunk of codes all together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883647#M349124</link>
      <description>&lt;P&gt;Try just adding FY as the FIRST by variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try running this code to do two fiscal years.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=final out=sub1;
  by fy caseload rc2;
  where fy in ('FY16/17' 'FY17/18') ;
run;

data sub1;
  set sub1;
  by fy caseload rc2;
  if first.rc2 then dcounter=0;
  if caseload lt 11 and caseload ne 0 then do;
    delete=1;
    dcounter+1;
  end;
  last_delete=ifn(first.rc2,.,lag(delete));
  if last_delete and dcounter lt 2 then delete=1;
run;

proc sql;
create table want1 as
  select *, max(dcounter) as maxd
  from sub1
  group by fy
;
quit;

data want1; set want1;
  if maxd=1 and delete=1 and last_delete=1 then caseload=caseload*-1;
  keep fy rc2 pos caseload;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If it works then just update the WHERE statement to select&amp;nbsp; all of the years you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  where 'FY16' &amp;lt;=: fy &amp;lt;: 'FY20' ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Jul 2023 23:46:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883647#M349124</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-05T23:46:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to loop through multiple years to process a chunk of codes all together</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883789#M349163</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;Thanks Tom! You were right, I added FY as a BY variable and figured out a code (without doing macro) that works.&lt;/P&gt;
&lt;PRE&gt;PROC SORT DATA=FINAL OUT=SUB1;
	BY FY CASELOAD RC2;
RUN;

DATA SUB1;
	SET SUB1;
	BY FY CASELOAD;
	IF FIRST.RC2 OR FIRST.FY THEN DCOUNTER=0;
	IF FIRST.FY THEN FYCNT=0;
	IF CASELOAD LT 11 AND CASELOAD NE 0 THEN DO;
		DELETE=1;
		DCOUNTER+1;
		FYCNT+1;
	END;
	LAST_DELETE=IFN(FIRST.RC2,.,LAG(DELETE));
	*IF LAST_DELETE AND DCOUNTER LT 2 THEN DELETE=1; 
RUN;

PROC SQL;
	CREATE TABLE WANT1 AS
	SELECT *, SUM(DELETE) AS MAXD 
FROM SUB1
GROUP BY FY
;	
QUIT;

PROC SORT DATA=WANT1; BY FY CASELOAD RC2; RUN;

DATA WANT1; SET WANT1;
	IF MAXD=1 AND LAST_DELETE=1 THEN CASELOAD=CASELOAD*-1;
	KEEP FY RC2 POS CASELOAD;
RUN;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code also works fine!&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Try just adding FY as the FIRST by variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try running this code to do two fiscal years.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=final out=sub1;
  by fy caseload rc2;
  where fy in ('FY16/17' 'FY17/18') ;
run;

data sub1;
  set sub1;
  by fy caseload rc2;
  if first.rc2 then dcounter=0;
  if caseload lt 11 and caseload ne 0 then do;
    delete=1;
    dcounter+1;
  end;
  last_delete=ifn(first.rc2,.,lag(delete));
  if last_delete and dcounter lt 2 then delete=1;
run;

proc sql;
create table want1 as
  select *, max(dcounter) as maxd
  from sub1
  group by fy
;
quit;

data want1; set want1;
  if maxd=1 and delete=1 and last_delete=1 then caseload=caseload*-1;
  keep fy rc2 pos caseload;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If it works then just update the WHERE statement to select&amp;nbsp; all of the years you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  where 'FY16' &amp;lt;=: fy &amp;lt;: 'FY20' ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Try just adding FY as the FIRST by variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try running this code to do two fiscal years.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=final out=sub1;
  by fy caseload rc2;
  where fy in ('FY16/17' 'FY17/18') ;
run;

data sub1;
  set sub1;
  by fy caseload rc2;
  if first.rc2 then dcounter=0;
  if caseload lt 11 and caseload ne 0 then do;
    delete=1;
    dcounter+1;
  end;
  last_delete=ifn(first.rc2,.,lag(delete));
  if last_delete and dcounter lt 2 then delete=1;
run;

proc sql;
create table want1 as
  select *, max(dcounter) as maxd
  from sub1
  group by fy
;
quit;

data want1; set want1;
  if maxd=1 and delete=1 and last_delete=1 then caseload=caseload*-1;
  keep fy rc2 pos caseload;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If it works then just update the WHERE statement to select&amp;nbsp; all of the years you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  where 'FY16' &amp;lt;=: fy &amp;lt;: 'FY20' ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Thu, 06 Jul 2023 18:01:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-loop-through-multiple-years-to-process-a-chunk-of-codes/m-p/883789#M349163</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2023-07-06T18:01:35Z</dc:date>
    </item>
  </channel>
</rss>

