<?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: Conditional first. &amp;amp; last. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/639985#M190480</link>
    <description>&lt;P&gt;Hey Chris,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is road crash data. The three one means the three people (rows) are each in the same accident (accident_id). Does that make sense?&lt;/P&gt;</description>
    <pubDate>Wed, 15 Apr 2020 06:31:17 GMT</pubDate>
    <dc:creator>e_______c</dc:creator>
    <dc:date>2020-04-15T06:31:17Z</dc:date>
    <item>
      <title>Conditional first. &amp; last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/639949#M190452</link>
      <description>&lt;P&gt;Hi &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a conditional variable (outcome) based on accident_id and road_user_type:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- if &lt;U&gt;anyone&lt;/U&gt; in an accident was a vulnerable road user &amp;gt; then outcome = 1;&lt;/P&gt;&lt;P&gt;- else if &lt;U&gt;everyone&lt;/U&gt; in an accident was a MVO &amp;gt; then outcome = 2;&lt;/P&gt;&lt;P&gt;- else outcome = 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dataset have;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Accident_ID&lt;/TD&gt;&lt;TD&gt;Road_user_type&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Vulnerable&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;MVO&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;MVO&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;MVO&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;MVO&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;MVO&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dataset want;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Accident_ID&lt;/TD&gt;&lt;TD&gt;Road_user_type&lt;/TD&gt;&lt;TD&gt;Outcome&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Vulnerable&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;MVO&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;MVO&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;MVO&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;MVO&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;MVO&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&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>Wed, 15 Apr 2020 02:55:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/639949#M190452</guid>
      <dc:creator>e_______c</dc:creator>
      <dc:date>2020-04-15T02:55:33Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional first. &amp; last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/639967#M190463</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Want;
Set Have;
If Road_user_type = "Vulnerable" then Outcome = 1;
If Road_user_type = "MVO" then Outcome = 2;
Else Outcome = 3;
Run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Apr 2020 05:12:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/639967#M190463</guid>
      <dc:creator>sdhilip</dc:creator>
      <dc:date>2020-04-15T05:12:23Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional first. &amp; last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/639984#M190479</link>
      <description>&lt;P&gt;Why are there three ones?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 06:26:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/639984#M190479</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-04-15T06:26:58Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional first. &amp; last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/639985#M190480</link>
      <description>&lt;P&gt;Hey Chris,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is road crash data. The three one means the three people (rows) are each in the same accident (accident_id). Does that make sense?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 06:31:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/639985#M190480</guid>
      <dc:creator>e_______c</dc:creator>
      <dc:date>2020-04-15T06:31:17Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional first. &amp; last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/640005#M190499</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by accident_id;
retain outcome;
if first.accident_id then do;
if road_user_type eq "Vulnerable" then outcome=1;
else if road_user_type eq "MVO" then outcome=2;
else if road_user_type eq "" then outcome=.;
else outcome=3;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hello, it s my proposition.&lt;/P&gt;&lt;P&gt;I saw in the case accident_id = 2, road_user_type =. &amp;gt;&amp;gt;&amp;gt; outcome =. &amp;gt;&amp;gt;&amp;gt; then I add a case where outcome =.&lt;BR /&gt;So we have 4 cases: outcome =., 1,2,3&lt;/P&gt;&lt;P&gt;Have a nice day.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 07:22:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/640005#M190499</guid>
      <dc:creator>kelxxx</dc:creator>
      <dc:date>2020-04-15T07:22:01Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional first. &amp; last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/640017#M190508</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 infile datalines;
 input Accident_ID Road_user_type $10.;
datalines;
1 Vulnerable
1 MVO
1 .
2 .
2 MVO
3 MVO
3 MVO
3 MVO
3 MVO
; run;
 
 data temp1;
 set have;
       if road_user_type = 'Vulnerable' then road_user_code=1; else
       if road_user_type = 'MVO' then road_user_code=2; else
         road_user_code=0;
run;

proc sort data=temp1; by Accident_ID road_user_code; run;

data temp2;
 set temp1;
    by Accident_ID ;
       retain outcome;
       if first.Accident_ID then outcome=.;
       if road_user_code = 1 then outcome=1;
       if outcome ne 1 then do;
          if first.Accident_ID then do;
             if road_user_code = 2
                then outcome = 2;
                else outcome = .;
          end; 
          else do;
             if road_user_code = 2 and
                outcome = 2 then;
             else outcome = .;
          end;
      end;
      if last.accident_id then output;
run;

data want;
 merge have temp2;
  by Accident_ID ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Apr 2020 07:50:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/640017#M190508</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-04-15T07:50:34Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional first. &amp; last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/640019#M190510</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/323007"&gt;@e_______c&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is another approach using a PROC SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;

	/* if anyone in an accident was a vulnerable road user &amp;gt; then outcome = 1 */
	(select *, 1 as outcome
	from have
	group by Accident_ID
	having sum(Road_user_type="Vulnerable") &amp;gt; 0)
	
	union all
	
	/* else if everyone in an accident was a MVO &amp;gt; then outcome = 2 */
	(select *, 2 as outcome
	from have
	group by Accident_ID
	having count(Accident_ID) = sum(Road_user_type="MVO"))

	union all
	
	/* else outcome = 3 */
	(select *, 3 as outcome
	from have
	group by Accident_ID
	having sum(Road_user_type="Vulnerable") = 0 and count(Accident_ID) &amp;gt; sum(Road_user_type="MVO"))
	
	order by Accident_ID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Apr 2020 07:51:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/640019#M190510</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-04-15T07:51:21Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional first. &amp; last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/640020#M190511</link>
      <description>&lt;P&gt;Per your rule, accident_id = 2 should have outcome 3.&lt;/P&gt;
&lt;P&gt;This code achieves that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Accident_ID Road_user_type :$10.;
datalines;
1 Vulnerable
1 MVO
1 .
2 .
2 MVO
3 MVO
3 MVO
3 MVO
;

data acc_type;
set have;
by accident_id;
retain flag_mvo flag_vuln;
if first.accident_id
then do;
  flag_mvo = 1;
  flag_vuln = 0;
end;
if road_user_type ne 'MVO' then flag_mvo = 0;
if road_user_type = 'Vulnerable' than flag_vuln = 1;
if last.accident_id;
if flag_vuln then outcome = 1;
else if flag_mvo then outcome = 2;
else outcome = 3;
keep accident_id outcome;
run;

data want;
merge
  have
  acc_type
;
by accident_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Apr 2020 07:52:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/640020#M190511</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-15T07:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional first. &amp; last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/640082#M190554</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select ACCIDENT_ID
       , ROAD_USER_TYPE 
       , case when sum(ROAD_USER_TYPE='Vulnerable')   then 1
              when sum(ROAD_USER_TYPE='MVO')=count(*) then 2
              else                                         3 end as OUTCOME
  from HAVE
  group by ACCIDENT_ID;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;TABLE class="table" style="border-collapse: collapse; border-spacing: 0px; margin-bottom: 1em; border-width: 1px 0px 0px 1px; border-style: solid; border-color: #c1c1c1; border-image: initial; margin-left: auto; margin-right: auto; color: #000000; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: center; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #fafbfe; text-decoration-style: initial; text-decoration-color: initial;" aria-label="Query Results"&gt;&lt;CAPTION aria-label="Query Results"&gt;&amp;nbsp;&lt;/CAPTION&gt;&lt;COLGROUP&gt;&lt;COL style="border-left: 1px solid #c1c1c1; border-right: 0px solid #c1c1c1;" /&gt;&lt;COL style="border-left: 1px solid #c1c1c1; border-right: 0px solid #c1c1c1;" /&gt;&lt;COL style="border-left: 1px solid #c1c1c1; border-right: 0px solid #c1c1c1;" /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR style="border-top: 1px solid #c1c1c1; border-bottom: 0px solid #c1c1c1;"&gt;
&lt;TH class="r b header" style="text-align: right; padding: 3px 6px; vertical-align: bottom; background-color: #edf2f9; border-color: #b0b7bb; border-style: solid; border-width: 0px 1px 1px 0px; color: #112277; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: bold;" scope="col"&gt;ACCIDENT_ID&lt;/TH&gt;
&lt;TH class="b header" style="text-align: left; padding: 3px 6px; vertical-align: bottom; background-color: #edf2f9; border-color: #b0b7bb; border-style: solid; border-width: 0px 1px 1px 0px; color: #112277; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: bold;" scope="col"&gt;ROAD_USER_TYPE&lt;/TH&gt;
&lt;TH class="r b header" style="text-align: right; padding: 3px 6px; vertical-align: bottom; background-color: #edf2f9; border-color: #b0b7bb; border-style: solid; border-width: 0px 1px 1px 0px; color: #112277; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: bold;" scope="col"&gt;OUTCOME&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR style="border-top: 1px solid #c1c1c1; border-bottom: 0px solid #c1c1c1;"&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;1&lt;/TD&gt;
&lt;TD class="data" style="text-align: left; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="border-top: 1px solid #c1c1c1; border-bottom: 0px solid #c1c1c1;"&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;1&lt;/TD&gt;
&lt;TD class="data" style="text-align: left; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;Vulnerable&lt;/TD&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="border-top: 1px solid #c1c1c1; border-bottom: 0px solid #c1c1c1;"&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;1&lt;/TD&gt;
&lt;TD class="data" style="text-align: left; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;MVO&lt;/TD&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="border-top: 1px solid #c1c1c1; border-bottom: 0px solid #c1c1c1;"&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;2&lt;/TD&gt;
&lt;TD class="data" style="text-align: left; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;MVO&lt;/TD&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="border-top: 1px solid #c1c1c1; border-bottom: 0px solid #c1c1c1;"&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;2&lt;/TD&gt;
&lt;TD class="data" style="text-align: left; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="border-top: 1px solid #c1c1c1; border-bottom: 0px solid #c1c1c1;"&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;3&lt;/TD&gt;
&lt;TD class="data" style="text-align: left; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;MVO&lt;/TD&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="border-top: 1px solid #c1c1c1; border-bottom: 0px solid #c1c1c1;"&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;3&lt;/TD&gt;
&lt;TD class="data" style="text-align: left; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;MVO&lt;/TD&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="border-top: 1px solid #c1c1c1; border-bottom: 0px solid #c1c1c1;"&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;3&lt;/TD&gt;
&lt;TD class="data" style="text-align: left; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;MVO&lt;/TD&gt;
&lt;TD class="r data" style="text-align: right; padding: 3px 6px; vertical-align: top; background-color: #ffffff; border-color: #c1c1c1; border-style: solid; border-width: 0px 1px 1px 0px; font-family: Arial, 'Albany AMT', Helvetica, Helv; font-size: x-small; font-style: normal; font-weight: normal;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 12:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/640082#M190554</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-04-15T12:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional first. &amp; last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/640141#M190593</link>
      <description>&lt;P&gt;You have a nice compact assignment rule, whose data needs can be satisfied with a self-merge, where each argument (except the last) of the merge statement selects a needed data subset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Accident_ID Road_user_type :$10.;
datalines;
1 Vulnerable
1 MVO
1 .
2 .
2 MVO
3 MVO
3 MVO
3 MVO
;

data want;
  merge 
      have (where=(road_user_type='Vulnerable') in=invul)
      have (where=(road_user_type=' ') in=inmiss)
      have (where=(road_user_type^='MVO') in=non_mvo)
      have ;
  by accident_id;
  if invul=1   then outcome=1; else
  if inmiss=1  then outcome=.; else
  if non_mvo=0 then outcome=2; else
  outcome=3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might ordinarily be worried about mixing data from differing observations in the merge.&amp;nbsp; But because the last argument of the merge is the complete set of records in HAVE, the individual values from those records will overwrite the value obtained from the matching subsets preceding it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;edit: Let me add some further notes on the MERGE statement with BY.&amp;nbsp; Although the merged subsets for each ID can have differing numbers of records from each other and from the full dataset, the IN= dummies will be constant for the entire BY-group.&amp;nbsp; I.e. if you have one record with &lt;EM&gt;&lt;STRONG&gt;road_user_type=' '&lt;/STRONG&gt;&lt;/EM&gt; (and therefore inmiss=1) vs 3 records in the entire BY group, you'll have 3 iterations of the data step (i.e. 3 output records in this case), and for ALL of those iterations, the INMISS will equal 1.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 17:21:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/640141#M190593</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-04-15T17:21:16Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional first. &amp; last.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/640253#M190652</link>
      <description>&lt;P&gt;Thanks mkeintz &amp;gt; that worked perfectly! Such a simple thing but I couldn't figure it out. Thanks again!&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 01:41:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-first-amp-last/m-p/640253#M190652</guid>
      <dc:creator>e_______c</dc:creator>
      <dc:date>2020-04-16T01:41:14Z</dc:date>
    </item>
  </channel>
</rss>

