<?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: Creating flag and additional record in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807157#M318164</link>
    <description>could you please help me on this .. its little bit urgent now</description>
    <pubDate>Mon, 11 Apr 2022 13:22:00 GMT</pubDate>
    <dc:creator>ambadi007</dc:creator>
    <dc:date>2022-04-11T13:22:00Z</dc:date>
    <item>
      <title>Creating flag and additional record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807116#M318135</link>
      <description>&lt;P&gt;Hi team,&lt;/P&gt;&lt;P&gt;i have a tricky requirement with 3 scenarios , scenario 1 i need to create an additional flag=y with the closest value of awtarget&lt;/P&gt;&lt;P&gt;Scenario 1 Same subject same param same avisitn with different ADY (AWDIFF=awtarget-ady)&lt;/P&gt;&lt;P&gt;USUBJID VISIT VISITNUM AVISIT AVISITN PARAM AVAL&amp;nbsp; &amp;nbsp;AWTARGET ADY AWDIFF&lt;BR /&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WEEK1 25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Week 1&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PAR1&amp;nbsp; &amp;nbsp;55&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WEEK 1 25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Week 1&amp;nbsp; &amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PAR1&amp;nbsp; &amp;nbsp; 50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;(closest values of AWDIFF with awtarget will be flagged FLG="Y" so here the first record should be flagged as Y&lt;BR /&gt;since the closest awdiff is low for that record )&lt;/P&gt;&lt;P&gt;Scenario 2 Same subject same param same avisitn with same ADY (AWDIFF=awtarget-ady)&lt;/P&gt;&lt;P&gt;USUBJID VISIT VISITNUM AVISIT AVISITN PARAM AVAL&amp;nbsp;AWTARGET ADY AWDIFF&lt;/P&gt;&lt;P&gt;102&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WEEK 1 25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Week 1&amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PAR1&amp;nbsp; &amp;nbsp; &amp;nbsp; 55&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;102&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WEEK 1 25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Week 1&amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PAR1&amp;nbsp; &amp;nbsp; &amp;nbsp;50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;(need to create an additional record with average of AVAL for these multiple records and create new variable dtype="AVERAGE" and FLG="Y" for that record)&lt;/P&gt;&lt;P&gt;Scenario 3 Same subject same param different avisitn with same ADY and AWDIFF=0&lt;/P&gt;&lt;P&gt;USUBJID VISIT VISITNUM AVISIT AVISITN AVAL PARAM AWTARGET ADY AWDIFF&lt;BR /&gt;103&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;UNS&amp;nbsp; &amp;nbsp; &amp;nbsp;20.01&amp;nbsp; &amp;nbsp; &amp;nbsp; Week 1&amp;nbsp; &amp;nbsp; 20.01&amp;nbsp; &amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp;PAR1 55&amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;103&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;UNS&amp;nbsp; &amp;nbsp; &amp;nbsp;20.02&amp;nbsp; &amp;nbsp; &amp;nbsp; Week 1 &amp;nbsp; &amp;nbsp;20.02&amp;nbsp; &amp;nbsp; 20&amp;nbsp; &amp;nbsp; PAR1 50&amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;103&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; UNS&amp;nbsp; &amp;nbsp; &amp;nbsp; 20.03&amp;nbsp; &amp;nbsp; &amp;nbsp; Week 1 &amp;nbsp; &amp;nbsp; 20.03&amp;nbsp; &amp;nbsp;&amp;nbsp;20&amp;nbsp; &amp;nbsp;PAR1 50&amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;need to create an additional record with average of AVAL for these multiple records and create new variable dtype="AVERAGE" and FLG="Y"&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;so the needed output is below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;USUBJID VISIT VISITNUM AVISIT AVISITN PARAM AVAL&amp;nbsp; AWTARGET ADY AWDIFF FLG DTYPE&lt;BR /&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WEEK 1 25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Week 1&amp;nbsp; &amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PAR1&amp;nbsp; &amp;nbsp; &amp;nbsp; 55&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; Y&lt;BR /&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WEEK 1 25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Week 1&amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PAR1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;102&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WEEK 1 25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Week 1&amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PAR1&amp;nbsp; &amp;nbsp; &amp;nbsp; 5 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;102&amp;nbsp; &amp;nbsp; &amp;nbsp; WEEK 1 25&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Week 1&amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PAR1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;102&amp;nbsp; &amp;nbsp; &amp;nbsp; WEEK 1 25&amp;nbsp; &amp;nbsp; &amp;nbsp; Week 1&amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PAR1&amp;nbsp; &amp;nbsp; &amp;nbsp; Avg&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp;Y&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; AVG&lt;BR /&gt;etc...&lt;/P&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>Mon, 11 Apr 2022 09:44:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807116#M318135</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2022-04-11T09:44:00Z</dc:date>
    </item>
    <item>
      <title>Re: Creating flag and additional record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807131#M318147</link>
      <description>&lt;P&gt;what should the avisitn be for scenario 3?&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2022 11:02:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807131#M318147</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2022-04-11T11:02:55Z</dc:date>
    </item>
    <item>
      <title>Re: Creating flag and additional record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807132#M318148</link>
      <description>We can give as 999</description>
      <pubDate>Mon, 11 Apr 2022 11:10:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807132#M318148</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2022-04-11T11:10:54Z</dc:date>
    </item>
    <item>
      <title>Re: Creating flag and additional record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807135#M318149</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76823"&gt;@ambadi007&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i have a tricky requirement with 3 scenarios , scenario 1 i need to create an additional flag=y with the closest value of awtarget&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Closest to what?&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2022 11:50:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807135#M318149</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-11T11:50:22Z</dc:date>
    </item>
    <item>
      <title>Re: Creating flag and additional record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807141#M318153</link>
      <description>The record which is having low awdiff will be close value</description>
      <pubDate>Mon, 11 Apr 2022 11:56:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807141#M318153</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2022-04-11T11:56:12Z</dc:date>
    </item>
    <item>
      <title>Re: Creating flag and additional record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807144#M318156</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76823"&gt;@ambadi007&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;The record which is having low awdiff will be close value&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Odd terminology. Why don't you just say the lowest value?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if on one row AWDIF is -7 and another row AWDIF is +1, then we want the -7&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2022 12:03:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807144#M318156</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-11T12:03:07Z</dc:date>
    </item>
    <item>
      <title>Re: Creating flag and additional record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807146#M318158</link>
      <description>yes for the first scenario we have to take the lowest value</description>
      <pubDate>Mon, 11 Apr 2022 12:05:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807146#M318158</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2022-04-11T12:05:01Z</dc:date>
    </item>
    <item>
      <title>Re: Creating flag and additional record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807157#M318164</link>
      <description>could you please help me on this .. its little bit urgent now</description>
      <pubDate>Mon, 11 Apr 2022 13:22:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807157#M318164</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2022-04-11T13:22:00Z</dc:date>
    </item>
    <item>
      <title>Re: Creating flag and additional record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807163#M318165</link>
      <description>&lt;P&gt;you could do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input USUBJID $ VISIT $ VISITNUM AVISIT $ AVISITN PARAM $ AVAL AWTARGET ADY AWDIFF;
datalines;
101 WEEK1 25 Week1 20 PAR1 55 5 4 1
101 WEEK1 25 Week1 20 PAR1 50 5 3 2
102 WEEK1 25 Week1 20 PAR1 55 5 4 1
102 WEEK1 25 Week1 20 PAR1 50 5 4 1
103 UNS 20.01 Week1 20.01 PAR1 55 5 5 0
103 UNS 20.02 Week1 20.02 PAR1 50 5 5 0
103 UNS 20.03 Week1 20.03 PAR1 50 5 5 0
;
run;

proc sort data=have; by usubjid param avisit descending AWDIFF; run;

data want;
set have;
length DTYPE $40;
call missing(dtype);
by usubjid param avisit descending AWDIFF;
*Define help vars;
retain n . countflg . avgn . avgsum . avgcalc . avisitnchg .;
*SC1;
if first.AWDIFF then countflg=0;
countflg+1;
if last.avisit and countflg=1 then flg='Y';

*SC2/3;
lag1_avisitn=lag1(avisitn);
if first.avisit then do;
   avgn=0;
   avgsum=0;
   avgcalc=0;
   avisitnchg=0;
   lag1_avisitn=.;
end;

avgn+1;
if avisitn ne lag1_avisitn then avisitnchg+1;
if not missing(aval) then avgsum=sum(avgsum,aval);
avgcalc=avgsum/avgn;

if last.awdiff and avgn&amp;gt;1 and countflg=avgn then do;
   output;   
   *SC2/3;
   avg=avgcalc;
   dtype='AVG';
   *SC3;
   if avisitnchg=avgn then avisitn=999;
   output;
end;
else output;
drop n countflg lag1_avisitn avgn avgsum avgcalc avisitnchg;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Apr 2022 13:42:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807163#M318165</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2022-04-11T13:42:10Z</dc:date>
    </item>
    <item>
      <title>Re: Creating flag and additional record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807186#M318177</link>
      <description>&lt;P&gt;Thank you for providing the solution for this tricky requirement. i have below query for the code . when the below criteria is coming it is not giving exact output . the below the output is provided&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;USUBJID VISIT&amp;nbsp; &amp;nbsp; &amp;nbsp;VISITNUM AVISIT&amp;nbsp; AVISITN PARAM AVAL AWTARGET ADY AWDIFF DTYPE FLG&lt;BR /&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Week20&amp;nbsp; &amp;nbsp; &amp;nbsp;20.01&amp;nbsp; &amp;nbsp; &amp;nbsp;Week 1&amp;nbsp; &amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PAR1&amp;nbsp; &amp;nbsp; &amp;nbsp; 55&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;141&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;169&amp;nbsp; &amp;nbsp; &amp;nbsp;28&lt;BR /&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; UNS20.02&amp;nbsp; 20.02&amp;nbsp; &amp;nbsp; Week 1&amp;nbsp; &amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PAR1&amp;nbsp; &amp;nbsp; &amp;nbsp; 50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 141&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;169&amp;nbsp; &amp;nbsp; 28&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF6600"&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; UNS20.02&amp;nbsp; 20.02&amp;nbsp; &amp;nbsp; Week 1&amp;nbsp; &amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PAR1&amp;nbsp; &amp;nbsp; &amp;nbsp; 50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 141&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;169&amp;nbsp; &amp;nbsp; 28&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AVG&lt;/FONT&gt;&lt;BR /&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Week44&amp;nbsp; &amp;nbsp; &amp;nbsp; 20.03&amp;nbsp; &amp;nbsp; Week 1&amp;nbsp; &amp;nbsp; 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PAR1&amp;nbsp; &amp;nbsp; &amp;nbsp;50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Y&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this scenario the Flag and Dtype are differently coming in output. the flag should come for the newly created record only (the highlighted record) . not for a different record.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2022 16:02:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807186#M318177</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2022-04-11T16:02:33Z</dc:date>
    </item>
    <item>
      <title>Re: Creating flag and additional record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807283#M318251</link>
      <description>&lt;P&gt;I've adapted the code so that scenario 1 would come into play with this data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input USUBJID $ VISIT $ VISITNUM AVISIT $ AVISITN PARAM $ AVAL AWTARGET ADY AWDIFF;
datalines;
101 WEEK1 25 Week1 20 PAR1 55 5 4 1
101 WEEK1 25 Week1 20 PAR1 50 5 3 2
102 WEEK1 25 Week1 20 PAR1 55 5 4 1
102 WEEK1 25 Week1 20 PAR1 50 5 4 1
103 UNS 20.01 Week1 20 PAR1 55 5 5 0
103 UNS 20.02 Week1 20 PAR1 50 5 5 0
103 UNS 20.03 Week1 20 PAR1 50 5 5 0
105 Week20 20.01 Week1 20 PAR1 55 141 169 28
105 UNS20.02 20.02 Week1 20 PAR1 50 141 169 28 
105 Week44 20.03 Week1 20 PAR1 50 5 5 0 
;
run;

proc sort data=have; by usubjid param avisitn descending AWDIFF; run;

data want;
set have;
length DTYPE $40;
call missing(dtype);
by usubjid param avisitn descending AWDIFF;
*Define help vars;
retain n . countflg . avgn . avgsum . avgcalc . avisitnchg .;
*SC1;
if first.AWDIFF then countflg=0;
countflg+1;
if last.avisitn and countflg=1 then flg='Y';

*SC2/3;
lag1_avisitn=lag1(avisitn);
if first.avisitn then do;
   avgn=0;
   avgsum=0;
   avgcalc=0;
   avisitnchg=0;
   lag1_avisitn=.;
end;

avgn+1;
if avisitn ne lag1_avisitn then avisitnchg+1;
if not missing(aval) then avgsum=sum(avgsum,aval);
avgcalc=avgsum/avgn;

if last.avisitn and avgn&amp;gt;1 and countflg=avgn and missing(flg) then do;
   output;   
   *SC2/3;
   avg=avgcalc;
   dtype='AVG';
   *SC3;
   if avisitnchg=avgn then avisitn=999;
   output;
end;
else output;
drop n countflg lag1_avisitn avgn avgsum avgcalc avisitnchg;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Apr 2022 06:12:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-flag-and-additional-record/m-p/807283#M318251</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2022-04-12T06:12:28Z</dc:date>
    </item>
  </channel>
</rss>

