<?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: Apply &amp;quot;at least&amp;quot; condition in data step in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986068#M43795</link>
    <description>&lt;P&gt;The advice from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;to represent Y and N with numeric values 1 and 0 &lt;STRONG&gt;should not be ignored&lt;/STRONG&gt;. This applies to all SAS programming that you do now and in the future. If you are creating output (like a report or table) that you need to show others and Y and N is required in the output, then use a custom format to display 1 as Y and 0 as N.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I am still unable to understand the stated logic that takes me from the input data to the desired data. It simply is not clear.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please try to write it again, in a new post in this thread (instead of correcting the original post) explaining the rules (in detail) you want to follow. As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;would say, we need RULES and not EXAMPLES (although sometimes we need both).&lt;/P&gt;</description>
    <pubDate>Tue, 07 Apr 2026 11:29:32 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2026-04-07T11:29:32Z</dc:date>
    <item>
      <title>Apply "at least" condition in data step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986063#M43792</link>
      <description>&lt;P&gt;Dear all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset with rules I have to apply.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, suppose to have the following dummy dataset:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Line $8. Other_trt :$20.;
cards;
0001 2nd line  Y
0001 3rd line   Y
0001 1st line   Y
0001 2nd line  N
0002 1st line   Y
0003 3rd line  Y
0003 1st line  N
0004 2nd line Y
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have to do the following: flag IDs not in (1st line) that have at least one "Other_trt" entry = "Y".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm used to do: "proc sort" and then "first.*" Since I'm in a data step with many other rules on the main dataset, I would like to not to end the data step with "run" to do proc sort and the other stuffs&amp;nbsp; but instead do the job in the same data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Desired output:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Line $8. Other_trt :$20.  Flag  $8.;
cards;
0001 2nd line  Y 1
0001 3rd line   Y 0
0001 1st line   Y  0
0001 2nd line  N  0
0002 1st line   Y  0
0003 3rd line  Y  1
0003 1st line  N  0
0004 2nd line Y  1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Rule: "AT LEAST ONE ID with Other_trt = "Y" with (having) Line not in 1st line"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I deal with it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2026 11:00:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986063#M43792</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-04-07T11:00:06Z</dc:date>
    </item>
    <item>
      <title>Re: Apply "at least" condition in data step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986064#M43793</link>
      <description>&lt;P&gt;Since it sounds like you need to mark multiple observations (not specified how) it would likely take two or possibly more passes depending on other rules using a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can come up with at least two different meanings for " flag IDs not in (1st line) that have at least one "Other_trt" entry = "Y". " It would be a good idea to show the desired result to make sure any interpretation we use would be correct for the problem as well as showing the value(s) of any flags set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that there quite a few places where using a numeric 1/0 instead of text Y/N or True/False are much easier to deal with. For example, SAS treats numeric 1 as True or Yes and 0 as False or No so you can SUM a number of values and get the count of Yes values. And the result of a comparison is returned as 1/0&amp;nbsp; so you don't have to add in logic to assign Y/N. A format can display the text value of "Y" or "N" or other text as desired for reporting for people to read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this were my problem I would likely use a separate step with either Proc SQL or possibly Proc Freq to get the desired counts of Other_trt values and merge that result with the DB data set by ID values to add in that Flag.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2026 10:52:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986064#M43793</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2026-04-07T10:52:52Z</dc:date>
    </item>
    <item>
      <title>Re: Apply "at least" condition in data step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986065#M43794</link>
      <description>Thank you very much for your reply. I edited the question with the desired output.</description>
      <pubDate>Tue, 07 Apr 2026 11:00:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986065#M43794</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-04-07T11:00:51Z</dc:date>
    </item>
    <item>
      <title>Re: Apply "at least" condition in data step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986068#M43795</link>
      <description>&lt;P&gt;The advice from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;to represent Y and N with numeric values 1 and 0 &lt;STRONG&gt;should not be ignored&lt;/STRONG&gt;. This applies to all SAS programming that you do now and in the future. If you are creating output (like a report or table) that you need to show others and Y and N is required in the output, then use a custom format to display 1 as Y and 0 as N.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I am still unable to understand the stated logic that takes me from the input data to the desired data. It simply is not clear.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please try to write it again, in a new post in this thread (instead of correcting the original post) explaining the rules (in detail) you want to follow. As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;would say, we need RULES and not EXAMPLES (although sometimes we need both).&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2026 11:29:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986068#M43795</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2026-04-07T11:29:32Z</dc:date>
    </item>
    <item>
      <title>Re: Apply "at least" condition in data step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986072#M43797</link>
      <description>&lt;P&gt;You write that you want to flag whole ID's, but then you have the flags set only for particular observations&amp;nbsp;&lt;EM&gt;within an ID&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;Please describe in detail for every observation in your result dataset why the flag is set as it is.&lt;/P&gt;
&lt;P&gt;Post this in a new answer,&amp;nbsp;&lt;STRONG&gt;do not modify your original post!&lt;/STRONG&gt; This is needed so it's easier to follow the flow of discussion.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2026 13:10:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986072#M43797</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2026-04-07T13:10:18Z</dc:date>
    </item>
    <item>
      <title>Re: Apply "at least" condition in data step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986074#M43798</link>
      <description>&lt;P&gt;If you want to flag ID's then why is the value of FLAG different on some of the observations for ID 0001 and 0003?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If not then why isn't FLAG=1 on the second observation?&lt;/P&gt;
&lt;P&gt;I has OTHER_TRT='Y' and it is not '1st line'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general if you want to generate a new variable for all of a BY group you have two choices.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Have all of the information needed for calculation available on the FIRST observation in the group.&amp;nbsp; Then RETAIN the NEW variable so its value stays the same on all observations in the BY group.&lt;/LI&gt;
&lt;LI&gt;Use two do loops to process the observations for each BY group twice. In the first loop gather the information needed to make the calculation.&amp;nbsp; And in the second loop re-read and output the observations so all of the observations in the BY group get the newly calculated value.&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until (last.id);
    set have;
    by id;
    * gather information ;
  end;
  * calculations at the BY group level ;
  do until (last.id);
    set have;
    by id;
    * calculations at the observation level ;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Apr 2026 14:33:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986074#M43798</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-04-07T14:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: Apply "at least" condition in data step</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986089#M43800</link>
      <description>&lt;P&gt;As Tom said, the second obs in your desired output should be flag=1.&lt;/P&gt;
&lt;P&gt;And using DOW skill suggested by Tom.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. Line $8. Other_trt :$20.;
cards;
0001 2nd line  Y
0001 3rd line   Y
0001 1st line   Y
0001 2nd line  N
0002 1st line   Y
0003 3rd line  Y
0003 1st line  N
0004 2nd line   Y
;
run;

data want;
 count=0;
 do until(last.id);
  set DB;
  by id;
  if Line ne '1st line' and Other_trt='Y' then count+1;
 end;
 do until(last.id);
  set DB;
  by id;
  flag=0;
  if count&amp;gt;0 and Line ne '1st line' and Other_trt='Y' then flag=1;
  output;
 end;
drop count;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Apr 2026 06:29:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Apply-quot-at-least-quot-condition-in-data-step/m-p/986089#M43800</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2026-04-08T06:29:55Z</dc:date>
    </item>
  </channel>
</rss>

