<?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: Create a variable (Yes/No) that reports events in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-Yes-No-that-reports-events/m-p/914314#M360269</link>
    <description>&lt;P&gt;This works with corrected data for your Main and Ds1 data sets. You did not indicate that ID should be read as a character value so would be all missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data ds1;
input ID $ age_event;
datalines;
a1	67
b2	89
b3	3
d2	0
;
run;
data main;
input ID $ last_agerecorded;
datalines;
a1	56
a2	67
b1	68
b2	72
b3	132
c2	121
c3	124
c4	58
d1	89
d2	95
e2	74
;
run;

/* requires both sets to be sorted by ID*/
data want;
   merge main (in=inmain)
         ds1  (in=inds1)
  ;
  by id;
  event=inds1;
  if inmain and not inds1 then age_event= last_agerecorded;

  drop last_agerecorded;
run;&lt;/PRE&gt;
&lt;P&gt;The Merge will align two data sets one a BY variable. However the data step doesn't work well if both sets have repeated values for the by variable in both sets.&lt;/P&gt;
&lt;P&gt;The IN= option creates a true/false (1/0) temporary variable that indicates whether the current observation has values from that set. So you can test conditionally for the case of observations in Main but not Ds1 as shown.&lt;/P&gt;
&lt;P&gt;Sine the Event variable is basically that it came from DS1 that is using the Inds1 to set the Event variable.&lt;/P&gt;</description>
    <pubDate>Fri, 02 Feb 2024 20:39:38 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-02-02T20:39:38Z</dc:date>
    <item>
      <title>Create a variable (Yes/No) that reports events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-Yes-No-that-reports-events/m-p/914290#M360259</link>
      <description>&lt;P&gt;I have two datasets.&lt;BR /&gt;Dataset1: age_event variable is the age at which each ID reported an event. IDs that did not reported events were not included in this dataset.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data ds1;
input ID age_event;
datalines;
a1	67
b2	89
b3	3
d2	0
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Dataset2: All IDs of the sample are reported. this dataset contains one row per ID. variable "last_agerecorded" is the at which each reported their last record.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data main;
input ID last_agerecorded;
datalines;
a1	56
a2	67
b1	68
b2	72
b3	132
c2	121
c3	124
c4	58
d1	89
d2	95
e2	74
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;We would like to create an "event" variable that equals to 1 if the event was reported and the corresponding age reported on "age_event". This is from dataset ds1. If an ID did not report an event, then event=0, and "age_event" should be equal to the age under "age_lastrecorded" from the dataset main. How can we get the required output below? Thank you.&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;age_event&lt;/TD&gt;&lt;TD&gt;event&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a1&lt;/TD&gt;&lt;TD&gt;67&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;a2&lt;/TD&gt;&lt;TD&gt;67&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;b1&lt;/TD&gt;&lt;TD&gt;68&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;b2&lt;/TD&gt;&lt;TD&gt;89&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;b3&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c2&lt;/TD&gt;&lt;TD&gt;121&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c3&lt;/TD&gt;&lt;TD&gt;124&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c4&lt;/TD&gt;&lt;TD&gt;58&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;d1&lt;/TD&gt;&lt;TD&gt;89&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;d2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;e2&lt;/TD&gt;&lt;TD&gt;74&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 02 Feb 2024 19:40:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-Yes-No-that-reports-events/m-p/914290#M360259</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2024-02-02T19:40:51Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable (Yes/No) that reports events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-Yes-No-that-reports-events/m-p/914309#M360268</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data ds1;
input ID $ age_event;
datalines;
a1	67
b2	89
b3	3
d2	0
;
run;

data main;
input ID $ last_agerecorded;
datalines;
a1	56
a2	67
b1	68
b2	72
b3	132
c2	121
c3	124
c4	58
d1	89
d2	95
e2	74
;
run;

proc sort data=ds1;
  by ID;
run;
proc sort data=main;
  by ID;
run;

data want;
  merge main ds1(in=e);
  by ID;
  event=e;
  age_event=coalesce(age_event,last_agerecorded);
  drop last_agerecorded;
run;

proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2024 20:29:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-Yes-No-that-reports-events/m-p/914309#M360268</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-02-02T20:29:43Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable (Yes/No) that reports events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-Yes-No-that-reports-events/m-p/914314#M360269</link>
      <description>&lt;P&gt;This works with corrected data for your Main and Ds1 data sets. You did not indicate that ID should be read as a character value so would be all missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data ds1;
input ID $ age_event;
datalines;
a1	67
b2	89
b3	3
d2	0
;
run;
data main;
input ID $ last_agerecorded;
datalines;
a1	56
a2	67
b1	68
b2	72
b3	132
c2	121
c3	124
c4	58
d1	89
d2	95
e2	74
;
run;

/* requires both sets to be sorted by ID*/
data want;
   merge main (in=inmain)
         ds1  (in=inds1)
  ;
  by id;
  event=inds1;
  if inmain and not inds1 then age_event= last_agerecorded;

  drop last_agerecorded;
run;&lt;/PRE&gt;
&lt;P&gt;The Merge will align two data sets one a BY variable. However the data step doesn't work well if both sets have repeated values for the by variable in both sets.&lt;/P&gt;
&lt;P&gt;The IN= option creates a true/false (1/0) temporary variable that indicates whether the current observation has values from that set. So you can test conditionally for the case of observations in Main but not Ds1 as shown.&lt;/P&gt;
&lt;P&gt;Sine the Event variable is basically that it came from DS1 that is using the Inds1 to set the Event variable.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2024 20:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-Yes-No-that-reports-events/m-p/914314#M360269</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-02-02T20:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable (Yes/No) that reports events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-Yes-No-that-reports-events/m-p/914321#M360272</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds1;
input ID $ age_event;
datalines;
a1 67
b2 89
b3 3
d2 0
;
run;

data main;
input ID $ last_agerecorded;
datalines;
a1 56
a2 67
b1 68
b2 72
b3 132
c2 121
c3 124
c4 58
d1 89
d2 95
e2 74
;
run;

proc sql;
 create table want as 
  select ID
  ,	     age_event
  ,	     1 as event
  from ds1
   outer union corresponding
  select ID
  ,	     last_agerecorded as age_event
  ,      0 as event
  from main
  where ID not in(select distinct ID from ds1)
 order by ID;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Feb 2024 20:53:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-Yes-No-that-reports-events/m-p/914321#M360272</guid>
      <dc:creator>ChanceTGardener</dc:creator>
      <dc:date>2024-02-02T20:53:28Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable (Yes/No) that reports events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-Yes-No-that-reports-events/m-p/914343#M360283</link>
      <description>&lt;P&gt;Thank you so much!&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2024 22:00:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-Yes-No-that-reports-events/m-p/914343#M360283</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2024-02-02T22:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable (Yes/No) that reports events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-Yes-No-that-reports-events/m-p/914345#M360285</link>
      <description>Thank you for explaining each step!</description>
      <pubDate>Fri, 02 Feb 2024 22:02:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-Yes-No-that-reports-events/m-p/914345#M360285</guid>
      <dc:creator>ANKH1</dc:creator>
      <dc:date>2024-02-02T22:02:48Z</dc:date>
    </item>
  </channel>
</rss>

