<?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: Do loop iterations based on the value of a variable, with 0-padding in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-iterations-based-on-the-value-of-a-variable-with-0/m-p/964607#M375654</link>
    <description>That's cool - I had no idea you could create zero-padded variable name suffixes by just specifying them like you did above.</description>
    <pubDate>Fri, 18 Apr 2025 17:05:37 GMT</pubDate>
    <dc:creator>quickbluefish</dc:creator>
    <dc:date>2025-04-18T17:05:37Z</dc:date>
    <item>
      <title>Do loop iterations based on the value of a variable, with 0-padding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-iterations-based-on-the-value-of-a-variable-with-0/m-p/964598#M375648</link>
      <description>&lt;P&gt;I'm trying to select cases for a cohort. In order to be selected, they must have a status code of 10 for each month of the year that the person was alive. If someone lived the entire year or died in December, then all 12 STATUS_CODE variables must = 10. But if they died in February, then only STATUS_CODE _01 and STATUS_CODE_02 must = 10. I'm trying to use a do loop, with a count that goes up to the month that the person died (or 12 if they didn't die), for the STATUS_CODE variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also need to reformat the do loop count variable so that a padding 0 is added to single digit months but not to double digit months (so Jan should be 01 instead of 1, but October is still 10 and 010).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I'm getting an error that my do loop count variable "i" is an invalid argument for SYSFUNC because "i" isn't a number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another issue I'm concerned about is whether later months will overwrite the selection flag variable. For example, case 2345 should not be selected because status code = 6 near the middle of the year. But since the last status code is 10, will that overwrite the selection flag variable? &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried to provide sample data, but&amp;nbsp; I can't get that to work either &lt;span class="lia-unicode-emoji" title=":pouting_face:"&gt;😡&lt;/span&gt;&amp;nbsp; All of the BENE_DEATH_DT values are apparently invalid, even though everything is in date9 format. And all values for STATUS_CODE_10 are invalid, even though they're exactly the same as all the other STATUS_CODE variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dsd dlm=',' truncover;
input BENE_ID BENE_DEATH_DT date9. VALID_DEATH_DT_SW $ STATUS_CODE_01 
	STATUS_CODE_02 STATUS_CODE_03 STATUS_CODE_04 STATUS_CODE_05
	STATUS_CODE_06 STATUS_CODE_07 STATUS_CODE_08 STATUS_CODE_09
	STATUS_CODE_10 STATUS_CODE_11 STATUS_CODE_12;
datalines;
1234,"",.,10,10,10,10,10,10,10,10,10,10,10,10	/*should_be_selected = 1*/
2345,"",.,10,10,10,10,10,10,6,10,10,10,10,10	/*should_be_selected = 0*/
3456,"V",04JUN2018,10,10,10,10,10,10,0,0,0,0,0,0	/*should_be_selected = 1*/
4567,"V",15DEC2018,10,10,10,10,10,10,10,10,10,10,10,10	/*should_be_selected = 1*/
5678,"V",08FEB2018,10,6,0,0,0,0,0,0,0,0,0,0	/*should_be_selected = 0*/
;RUN;

DATA want; SET have;
IF VALID_DEATH_DT_SW = "V" THEN final_month = month(BENE_DEATH_DT);
	ELSE IF VALID_DEATH_DT_SW ^= "V" THEN final_month=12;
should_be_selected = 1;	
	
do i=1 to final_month;
	%let m=%sysfunc(putn(i,z2.)); /*This should add a padding 0 to single digit numbers*/
	IF STATUS_CODE_&amp;amp;m. ^= 10 THEN should_be_selected = 0;
end;	

RUN;&lt;BR /&gt;&lt;BR /&gt;proc&amp;nbsp;print&amp;nbsp;data=want;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Apr 2025 16:15:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-iterations-based-on-the-value-of-a-variable-with-0/m-p/964598#M375648</guid>
      <dc:creator>Wolverine</dc:creator>
      <dc:date>2025-04-18T16:15:24Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop iterations based on the value of a variable, with 0-padding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-iterations-based-on-the-value-of-a-variable-with-0/m-p/964601#M375650</link>
      <description>&lt;P&gt;Here's is the code again with a slightly better version of the datalines... one of the variables was in the wrong order. But most of the problems remain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dsd dlm=',' truncover;
input BENE_ID VALID_DEATH_DT_SW $ BENE_DEATH_DT date9. STATUS_CODE_01 
	STATUS_CODE_02 STATUS_CODE_03 STATUS_CODE_04 STATUS_CODE_05
	STATUS_CODE_06 STATUS_CODE_07 STATUS_CODE_08 STATUS_CODE_09
	STATUS_CODE_10 STATUS_CODE_11 STATUS_CODE_12;
datalines;
1234,"",.,10,10,10,10,10,10,10,10,10,10,10,10	/*should_be_selected = 1*/
2345,"",.,10,10,10,10,10,10,6,10,10,10,10,10	/*should_be_selected = 0*/
3456,"V",04JUN2018,10,10,10,10,10,10,0,0,0,0,0,0	/*should_be_selected = 1*/
4567,"V",15DEC2018,10,10,10,10,10,10,10,10,10,10,10,10	/*should_be_selected = 1*/
5678,"V",08FEB2018,10,6,0,0,0,0,0,0,0,0,0,0	/*should_be_selected = 0*/
;RUN;

DATA want; SET have;
IF VALID_DEATH_DT_SW = "V" THEN final_month = month(BENE_DEATH_DT);
	ELSE IF VALID_DEATH_DT_SW ^= "V" THEN final_month=12;
should_be_selected = 1;	
	
do i=1 to final_month;
	%let m=%sysfunc(putn(i,z2.)); /*This should add a padding 0 to single digit numbers*/
	IF STATUS_CODE_&amp;amp;m. ^= 10 THEN should_be_selected = 0;
end;	

RUN;proc print data=want;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Apr 2025 16:34:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-iterations-based-on-the-value-of-a-variable-with-0/m-p/964601#M375650</guid>
      <dc:creator>Wolverine</dc:creator>
      <dc:date>2025-04-18T16:34:51Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop iterations based on the value of a variable, with 0-padding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-iterations-based-on-the-value-of-a-variable-with-0/m-p/964605#M375653</link>
      <description>&lt;P&gt;Remember that the macro processor is a PRE-processor.&amp;nbsp; It takes the text of your program and changes it and then passes the results onto to the actual SAS language processor to evaluate and run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So placing that %LET statement in the middle of a data step makes no sense.&amp;nbsp; Move it to BEFORE the DATA statement since that is where it is actually going to end up executing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You want to use an ARRAY instead.&amp;nbsp; No code generation needed. So no macro code needed.&amp;nbsp; Also you probably want to deal with the missing values.&lt;/P&gt;
&lt;P&gt;Perhaps something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I cleaned up your example data step. Note you do NOT want to read the date variable using FORMATTED mode.&amp;nbsp; Use LIST MODE since you have a delimited input file.&amp;nbsp; So add the colon modifier before the DATE informat.&amp;nbsp; And you cannot have those strings in the last field on the data lines.&amp;nbsp; Why not just add the values from the comments as another variable which will make testing easier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dsd dlm=',' truncover;
  input BENE_ID VALID_DEATH_DT_SW $ BENE_DEATH_DT :date. 
        STATUS_CODE_01 - STATUS_CODE_12
        expected
  ;
  format BENE_DEATH_DT date9.;
datalines;
1234,"",,10,10,10,10,10,10,10,10,10,10,10,10,1
2345,"",,10,10,10,10,10,10,6,10,10,10,10,10,0
3456,"V",04JUN2018,10,10,10,10,10,10,0,0,0,0,0,0,1
4567,"V",15DEC2018,10,10,10,10,10,10,10,10,10,10,10,10,1
5678,"V",08FEB2018,10,6,0,0,0,0,0,0,0,0,0,0,0
;

DATA want;
  SET have;
  IF VALID_DEATH_DT_SW = "V" THEN final_month = month(BENE_DEATH_DT);
  ELSE IF VALID_DEATH_DT_SW ^= "V" THEN final_month=12;
  array status_code STATUS_CODE_01 - STATUS_CODE_12;
  should_be_selected = 1; 
  do i=1 to final_month;
    IF STATUS_CODE[i] not in (. 10) THEN should_be_selected = 0;
  end; 
  drop i;
RUN;

proc print data=want;
 var bene_id expected should_be_selected;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                              should_be_
Obs    BENE_ID    expected     selected

 1       1234         1            1
 2       2345         0            0
 3       3456         1            1
 4       4567         1            1
 5       5678         0            0
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Apr 2025 17:06:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-iterations-based-on-the-value-of-a-variable-with-0/m-p/964605#M375653</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-04-18T17:06:01Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop iterations based on the value of a variable, with 0-padding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-iterations-based-on-the-value-of-a-variable-with-0/m-p/964607#M375654</link>
      <description>That's cool - I had no idea you could create zero-padded variable name suffixes by just specifying them like you did above.</description>
      <pubDate>Fri, 18 Apr 2025 17:05:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-iterations-based-on-the-value-of-a-variable-with-0/m-p/964607#M375654</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-04-18T17:05:37Z</dc:date>
    </item>
  </channel>
</rss>

