<?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: write loop over multiple variables and flag if conditions are met in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822700#M324862</link>
    <description>&lt;P&gt;I was trying to first lag the time period and then if that does not increase the running total then trying to use the scan function to look if the substring for each word in the combined column matches the lag combined variable.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;sorry it's quite complicated&lt;/P&gt;</description>
    <pubDate>Mon, 11 Jul 2022 22:21:14 GMT</pubDate>
    <dc:creator>nyy2</dc:creator>
    <dc:date>2022-07-11T22:21:14Z</dc:date>
    <item>
      <title>write loop over multiple variables and flag if conditions are met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822648#M324846</link>
      <description>&lt;P&gt;hello! I am trying to write a loop to flag two criteria. First if the difference between lag(timeperiod) - timeperiod &amp;gt;=4 and then to compare if diag codes between time periods is the same of different. I want to run the loop over each patientID until one of these criteria's are met.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TIA!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;E.g. data&amp;nbsp;&lt;/P&gt;&lt;P&gt;patientID, timeperiod, combined&lt;/P&gt;&lt;P&gt;wh7,1, diag1_diag2_diag3&lt;/P&gt;&lt;P&gt;wh7, 4, diag1_diag2_diag3_diag4&lt;/P&gt;&lt;P&gt;wh7, 10,&amp;nbsp;diag1_diag2&lt;/P&gt;&lt;P&gt;wh7&lt;SPAN&gt;&amp;nbsp;15, diag4_diag10&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;wh4, 2, diag5_diag11_diag16&lt;/P&gt;&lt;P&gt;wh4, 4, diag5_diag11&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2022 20:39:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822648#M324846</guid>
      <dc:creator>nyy2</dc:creator>
      <dc:date>2022-07-11T20:39:09Z</dc:date>
    </item>
    <item>
      <title>Re: write loop over multiple variables and flag if conditions are met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822694#M324860</link>
      <description>&lt;P&gt;I'm assuming the data shown is input.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you show what you've tried so far?&lt;/P&gt;
&lt;P&gt;Can you show what you're looking for as output if this is the data input?&lt;/P&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/429363"&gt;@nyy2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;hello! I am trying to write a loop to flag two criteria. First if the difference between lag(timeperiod) - timeperiod &amp;gt;=4 and then to compare if diag codes between time periods is the same of different. I want to run the loop over each patientID until one of these criteria's are met.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;TIA!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;E.g. data&amp;nbsp;&lt;/P&gt;
&lt;P&gt;patientID, timeperiod, combined&lt;/P&gt;
&lt;P&gt;wh7,1, diag1_diag2_diag3&lt;/P&gt;
&lt;P&gt;wh7, 4, diag1_diag2_diag3_diag4&lt;/P&gt;
&lt;P&gt;wh7, 10,&amp;nbsp;diag1_diag2&lt;/P&gt;
&lt;P&gt;wh7&lt;SPAN&gt;&amp;nbsp;15, diag4_diag10&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;wh4, 2, diag5_diag11_diag16&lt;/P&gt;
&lt;P&gt;wh4, 4, diag5_diag11&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2022 21:49:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822694#M324860</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-07-11T21:49:32Z</dc:date>
    </item>
    <item>
      <title>Re: write loop over multiple variables and flag if conditions are met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822697#M324861</link>
      <description>&lt;P&gt;Yes this is example of input. For output, I would just want to add a column called running_total that would start at 1 and only increase when either time period triggered an increase of 1 or the different diag codes.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2022 22:03:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822697#M324861</guid>
      <dc:creator>nyy2</dc:creator>
      <dc:date>2022-07-11T22:03:14Z</dc:date>
    </item>
    <item>
      <title>Re: write loop over multiple variables and flag if conditions are met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822700#M324862</link>
      <description>&lt;P&gt;I was trying to first lag the time period and then if that does not increase the running total then trying to use the scan function to look if the substring for each word in the combined column matches the lag combined variable.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;sorry it's quite complicated&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2022 22:21:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822700#M324862</guid>
      <dc:creator>nyy2</dc:creator>
      <dc:date>2022-07-11T22:21:14Z</dc:date>
    </item>
    <item>
      <title>Re: write loop over multiple variables and flag if conditions are met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822701#M324863</link>
      <description>&lt;P&gt;Sorry, I think I'd need more details. Can you provide a fully worked example that shows the input and expected output (as data sets)?&lt;/P&gt;
&lt;P&gt;For example, it's not clear how this would be defined:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;"then to compare if diag codes between time periods is the same of different."&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;SAS data steps loop automatically so you don't need to really factor that in. For the daily difference that's easy.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;EDIT: to add running total instead of a counter&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by PatientID;

period_dif = dif(TimePeriod);
if first.patientID then counter=1;
else if period_dif&amp;gt;=4 then counter+1;


*assuming you do not want this in final data set;
*drop period_dif;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2022 22:32:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822701#M324863</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-07-11T22:32:29Z</dc:date>
    </item>
    <item>
      <title>Re: write loop over multiple variables and flag if conditions are met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822702#M324864</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/429363"&gt;@nyy2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;sorry it's quite complicated&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Ergo the request for an example of the output. Data/examples really help &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;EDIT: I've edited my previous response to essentially add the case counter. Not sure about the diagnosis stuff yet, please show an example. Also, did you happen to create this variable or is it stored that way initially? How many possible diagnosis are possible?&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2022 22:33:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822702#M324864</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-07-11T22:33:24Z</dc:date>
    </item>
    <item>
      <title>Re: write loop over multiple variables and flag if conditions are met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822703#M324865</link>
      <description>&lt;P&gt;Thanks, I added an example with some dummy data and notes of how I was thinking about it. List of codes in the combined does not really have a limit as I'm looking for any codes that occur within that time period.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2022 22:42:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822703#M324865</guid>
      <dc:creator>nyy2</dc:creator>
      <dc:date>2022-07-11T22:42:47Z</dc:date>
    </item>
    <item>
      <title>Re: write loop over multiple variables and flag if conditions are met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822705#M324866</link>
      <description>&lt;TABLE width="1065px"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="90.4375px"&gt;patientID&lt;/TD&gt;
&lt;TD width="115.844px"&gt;time_period&lt;/TD&gt;
&lt;TD width="190.969px"&gt;combined&lt;/TD&gt;
&lt;TD width="103.953px"&gt;running_total&lt;/TD&gt;
&lt;TD width="479.734px"&gt;Note&lt;/TD&gt;
&lt;TD width="83.0625px"&gt;Questions&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="90.4375px"&gt;wh7&lt;/TD&gt;
&lt;TD width="115.844px"&gt;1&lt;/TD&gt;
&lt;TD width="190.969px"&gt;C400_C401_C408&lt;/TD&gt;
&lt;TD width="103.953px"&gt;1&lt;/TD&gt;
&lt;TD width="479.734px"&gt;First record&lt;/TD&gt;
&lt;TD width="83.0625px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="90.4375px"&gt;wh7&lt;/TD&gt;
&lt;TD width="115.844px"&gt;4&lt;/TD&gt;
&lt;TD width="190.969px"&gt;C400_C401_C408_C409&lt;/TD&gt;
&lt;TD width="103.953px"&gt;2&lt;/TD&gt;
&lt;TD width="479.734px"&gt;Flag because previous row diagnosis codes are different&lt;/TD&gt;
&lt;TD width="83.0625px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="90.4375px"&gt;wh7&lt;/TD&gt;
&lt;TD width="115.844px"&gt;10&lt;/TD&gt;
&lt;TD width="190.969px"&gt;C400_C401_C408_C409&lt;/TD&gt;
&lt;TD width="103.953px"&gt;2&lt;/TD&gt;
&lt;TD width="479.734px"&gt;Total stays 2 because one of 2 conditions has been met, could drop out of loop here&lt;/TD&gt;
&lt;TD width="83.0625px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="90.4375px"&gt;wh7&lt;/TD&gt;
&lt;TD width="115.844px"&gt;15&lt;/TD&gt;
&lt;TD width="190.969px"&gt;C400_C401_C408&lt;/TD&gt;
&lt;TD width="103.953px"&gt;2&lt;/TD&gt;
&lt;TD width="479.734px"&gt;Total stays 2 because one of 2 conditions has been met, could drop out of loop here&lt;/TD&gt;
&lt;TD width="83.0625px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="90.4375px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="115.844px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="190.969px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="103.953px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="479.734px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="83.0625px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="90.4375px"&gt;wh4&lt;/TD&gt;
&lt;TD width="115.844px"&gt;1&lt;/TD&gt;
&lt;TD width="190.969px"&gt;C030_C039_C031&lt;/TD&gt;
&lt;TD width="103.953px"&gt;1&lt;/TD&gt;
&lt;TD width="479.734px"&gt;First record for this patient&lt;/TD&gt;
&lt;TD width="83.0625px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="90.4375px"&gt;wh4&lt;/TD&gt;
&lt;TD width="115.844px"&gt;2&lt;/TD&gt;
&lt;TD width="190.969px"&gt;C030_C039&lt;/TD&gt;
&lt;TD width="103.953px"&gt;1&lt;/TD&gt;
&lt;TD width="479.734px"&gt;Flag stays 1 because diag only dropped not added and no time break &amp;gt;4&lt;/TD&gt;
&lt;TD width="83.0625px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="90.4375px"&gt;wh4&lt;/TD&gt;
&lt;TD width="115.844px"&gt;5&lt;/TD&gt;
&lt;TD width="190.969px"&gt;C030_C049&lt;/TD&gt;
&lt;TD width="103.953px"&gt;2&lt;/TD&gt;
&lt;TD width="479.734px"&gt;Increase by one because diag changed from previous row&lt;/TD&gt;
&lt;TD width="83.0625px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="90.4375px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="115.844px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="190.969px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="103.953px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="479.734px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="83.0625px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="90.4375px"&gt;wh5&lt;/TD&gt;
&lt;TD width="115.844px"&gt;1&lt;/TD&gt;
&lt;TD width="190.969px"&gt;F107_F108&lt;/TD&gt;
&lt;TD width="103.953px"&gt;1&lt;/TD&gt;
&lt;TD width="479.734px"&gt;First record&lt;/TD&gt;
&lt;TD width="83.0625px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="90.4375px"&gt;wh5&lt;/TD&gt;
&lt;TD width="115.844px"&gt;3&lt;/TD&gt;
&lt;TD width="190.969px"&gt;F107_F109&lt;/TD&gt;
&lt;TD width="103.953px"&gt;2&lt;/TD&gt;
&lt;TD width="479.734px"&gt;Flag because previous row diagnosis codes are different&lt;/TD&gt;
&lt;TD width="83.0625px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="90.4375px"&gt;wh5&lt;/TD&gt;
&lt;TD width="115.844px"&gt;5&lt;/TD&gt;
&lt;TD width="190.969px"&gt;F107_F109&lt;/TD&gt;
&lt;TD width="103.953px"&gt;2&lt;/TD&gt;
&lt;TD width="479.734px"&gt;Total stays 2 because codes are same and condition has already been met, could drop out of loop here&lt;/TD&gt;
&lt;TD width="83.0625px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="90.4375px"&gt;wh5&lt;/TD&gt;
&lt;TD width="115.844px"&gt;7&lt;/TD&gt;
&lt;TD width="190.969px"&gt;F107_F108_F109&lt;/TD&gt;
&lt;TD width="103.953px"&gt;2&lt;/TD&gt;
&lt;TD width="479.734px"&gt;Total stays 2 because already met one of the conditions&lt;/TD&gt;
&lt;TD width="83.0625px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Attachments are a pain, please post directly into the forum. FYI - you cannot drop out of a loop in a data step, it loops through all rows.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2022 22:52:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822705#M324866</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-07-11T22:52:09Z</dc:date>
    </item>
    <item>
      <title>Re: write loop over multiple variables and flag if conditions are met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822722#M324870</link>
      <description>&lt;P&gt;okay thanks for letting me know. first time using the forum.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jul 2022 01:06:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822722#M324870</guid>
      <dc:creator>nyy2</dc:creator>
      <dc:date>2022-07-12T01:06:30Z</dc:date>
    </item>
    <item>
      <title>Re: write loop over multiple variables and flag if conditions are met</title>
      <link>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822894#M324938</link>
      <description>&lt;P&gt;Not 100% clear on your criteria as some things don't make sense to me but this shows how to check for the difference in time and a new diagnosis.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should be able to create your variable as desired with this information.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dlm='|';
input patientID $	time_period	combined  : $40.	running_total ;
cards;	
wh7|	1|	C400_C401_C408|	1	 
wh7|	4|	C400_C401_C408_C409|	2	
wh7|	10|	C400_C401_C408_C409|	2	
wh7|	15|	C400_C401_C408|	2		 	 	 	 	 
wh4|	1|	C030_C039_C031|	1	
wh4|	2|	C030_C039|	1	 
wh4|	5|	C030_C049|	2		 	 	 	 	 
wh5|	1|	F107_F108|	1	
wh5|	3|	F107_F109|	2	
wh5|	5|	F107_F109|	2	
wh5|	7|	F107_F108_F109|2	
;;;;
run;


data want;
set have (rename=running_total = total_check);
by patientID notsorted;
retain diag_list;

if first.patientID then do; diag_list = combined; running_total = 1; end;

dif_time = dif(time_period);

nwords = countw(combined, '_');

*see if new diagnosis is in the list;
diag_found=0;
if not first.patientID then do i=1 to nwords;
diag = scan(combined, i, '_');
if  findw(diag_list, diag, '_', 't')=0 then diag_not_found=1;
end;

if diag_not_found then do;
    running_total+1;
    diag_list = combined;
end;


drop i diag;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Jul 2022 16:31:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/write-loop-over-multiple-variables-and-flag-if-conditions-are/m-p/822894#M324938</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-07-12T16:31:01Z</dc:date>
    </item>
  </channel>
</rss>

