<?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: How to output to differing data sets if a value is present across any observations by id in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953784#M372573</link>
    <description>&lt;P&gt;Sort by ID and DESCENDING type. Then if there's any 1's it will be at the top and you can decide where to output the record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
input id date :yymmdd10.  type;
format date yymmdd10.;
datalines;
1 2020-01-01 0
1 2018-07-06 0
2 2015-02-15 1
3 2020-02-01 0
3 2021-07-05 1
3 2021-08-09 0
4 2020-01-30 0
5 2018-10-10 1
5 2019-10-11 1
;
RUN;

proc sort data=a;
by id descending type;
run;

data zerotype onetype;
set a;
by id descending type;

retain output_type;
length output_type $8.;

if first.id and type=1 then output_type='one';
else if first.id and type=0 then output_type='zero';

if output_type = 'zero' then output zerotype;
if output_type = 'one' then output onetype;

drop output_type;

run;

proc sort data=zerotype; by id date; run;
proc sort data=onetype; by id date; run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/458102"&gt;@sasgorilla&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a data set that has IDs, dates, (other variables excluded below), and a "type" variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
input id date :yymmdd10.  type;
format date yymmdd10.;
datalines;
1 2020-01-01 0
1 2018-07-06 0
2 2015-02-15 1
3 2020-02-01 0
3 2021-07-05 1
3 2021-08-09 0
4 2020-01-30 0
5 2018-10-10 1
5 2019-10-11 1
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If an ID ONLYhas a type value of "0", I want those id's observations to go to one dataset. If an id EVER has values of "1" I want those observations to go to another data set. See below:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data zerotype;
input id date :yymmdd10. type;
format date yymmdd10.;
datalines;
1 2020-01-01 0
1 2018-07-06 0
4 2020-01-30 0
;
run;

data onetype;
input id date :yymmdd10.  type;
format date yymmdd10.;
datalines;
2 2015-02-15 1
3 2020-02-01 0
3 2021-07-05 1
3 2021-08-09 0
5 2018-10-10 1
5 2019-10-11 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How could I do this in a datastep?&amp;nbsp; Thank you!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 17 Dec 2024 03:59:47 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2024-12-17T03:59:47Z</dc:date>
    <item>
      <title>How to output to differing data sets if a value is present across any observations by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953773#M372566</link>
      <description>&lt;P&gt;I have a data set that has IDs, dates, (other variables excluded below), and a "type" variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
input id date :yymmdd10.  type;
format date yymmdd10.;
datalines;
1 2020-01-01 0
1 2018-07-06 0
2 2015-02-15 1
3 2020-02-01 0
3 2021-07-05 1
3 2021-08-09 0
4 2020-01-30 0
5 2018-10-10 1
5 2019-10-11 1
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If an ID ONLYhas a type value of "0", I want those id's observations to go to one dataset. If an id EVER has values of "1" I want those observations to go to another data set. See below:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data zerotype;
input id date :yymmdd10. type;
format date yymmdd10.;
datalines;
1 2020-01-01 0
1 2018-07-06 0
4 2020-01-30 0
;
run;

data onetype;
input id date :yymmdd10.  type;
format date yymmdd10.;
datalines;
2 2015-02-15 1
3 2020-02-01 0
3 2021-07-05 1
3 2021-08-09 0
5 2018-10-10 1
5 2019-10-11 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How could I do this in a datastep?&amp;nbsp; Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2024 02:18:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953773#M372566</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-12-17T02:18:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to output to differing data sets if a value is present across any observations by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953779#M372569</link>
      <description>&lt;P&gt;There are probably more elegant ways but you could try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
input id date :yymmdd10.  type;
format date yymmdd10.;
datalines;
1 2020-01-01 0
1 2018-07-06 0
2 2015-02-15 1
3 2020-02-01 0
3 2021-07-05 1
3 2021-08-09 0
4 2020-01-30 0
5 2018-10-10 1
5 2019-10-11 1
;
RUN;
data onetype
     zerotype;
  keep id date type;
  merge Have (in = H1)
        Have (in = H2 
              where = (type2 = 1)
              keep = id type
              rename = (type = type2))
        ;
  by id;
  retain onetype;
  if first.id then do;
     if H2 then onetype = 1;
     else onetype = 0;
  end;
  if onetype then output onetype;
  else output zerotype; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2024 03:26:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953779#M372569</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2024-12-17T03:26:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to output to differing data sets if a value is present across any observations by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953780#M372570</link>
      <description>&lt;P&gt;That works, but your data step is too complex.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can just use the IN= flag (or the TYPE2 copy of TYPE) to drive the decision.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data onetype
     zerotype
;
  keep id date type;
  merge Have (in = H1)
        Have (in = H2 
              keep = id type
              rename = (type = type2)
              where = (type2 = 1)
             )
  ;
  by id;
  if H2 then output onetype;
  else output zerotype;
  drop type2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Dec 2024 03:38:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953780#M372570</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-12-17T03:38:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to output to differing data sets if a value is present across any observations by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953781#M372571</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Will this work with the WHERE= that you have:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; where = (type2 = 1)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or will it require referring to the original variable name?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; where = (type = 1)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2024 03:51:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953781#M372571</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-12-17T03:51:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to output to differing data sets if a value is present across any observations by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953784#M372573</link>
      <description>&lt;P&gt;Sort by ID and DESCENDING type. Then if there's any 1's it will be at the top and you can decide where to output the record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
input id date :yymmdd10.  type;
format date yymmdd10.;
datalines;
1 2020-01-01 0
1 2018-07-06 0
2 2015-02-15 1
3 2020-02-01 0
3 2021-07-05 1
3 2021-08-09 0
4 2020-01-30 0
5 2018-10-10 1
5 2019-10-11 1
;
RUN;

proc sort data=a;
by id descending type;
run;

data zerotype onetype;
set a;
by id descending type;

retain output_type;
length output_type $8.;

if first.id and type=1 then output_type='one';
else if first.id and type=0 then output_type='zero';

if output_type = 'zero' then output zerotype;
if output_type = 'one' then output onetype;

drop output_type;

run;

proc sort data=zerotype; by id date; run;
proc sort data=onetype; by id date; run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/458102"&gt;@sasgorilla&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a data set that has IDs, dates, (other variables excluded below), and a "type" variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
input id date :yymmdd10.  type;
format date yymmdd10.;
datalines;
1 2020-01-01 0
1 2018-07-06 0
2 2015-02-15 1
3 2020-02-01 0
3 2021-07-05 1
3 2021-08-09 0
4 2020-01-30 0
5 2018-10-10 1
5 2019-10-11 1
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If an ID ONLYhas a type value of "0", I want those id's observations to go to one dataset. If an id EVER has values of "1" I want those observations to go to another data set. See below:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data zerotype;
input id date :yymmdd10. type;
format date yymmdd10.;
datalines;
1 2020-01-01 0
1 2018-07-06 0
4 2020-01-30 0
;
run;

data onetype;
input id date :yymmdd10.  type;
format date yymmdd10.;
datalines;
2 2015-02-15 1
3 2020-02-01 0
3 2021-07-05 1
3 2021-08-09 0
5 2018-10-10 1
5 2019-10-11 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How could I do this in a datastep?&amp;nbsp; Thank you!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2024 03:59:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953784#M372573</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2024-12-17T03:59:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to output to differing data sets if a value is present across any observations by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953786#M372574</link>
      <description>&lt;P&gt;And here another option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data zerotype onetype;
	if _n_=1 then
		do;
			dcl hash h1(dataset:'have(where=(type=1))');
			h1.defineKey('id');
			h1.defineDone();
		end;
	set have;
	if h1.check()=0 then output onetype;
	else output zerotype;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Dec 2024 04:36:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953786#M372574</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-12-17T04:36:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to output to differing data sets if a value is present across any observations by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953787#M372575</link>
      <description>&lt;P&gt;This worked perfectly and was the briefest way to do. Admittedly, this is the most difficult code for me to understand as I don't recognize things like dcl, hash, h1 and the definekey/definedone. I see it is known as the hash object and I will look into it further.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks again!&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2024 05:11:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953787#M372575</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2024-12-17T05:11:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to output to differing data sets if a value is present across any observations by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953798#M372580</link>
      <description>&lt;P&gt;If you are not familiar with Hash Table, try PROC SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
input id date :yymmdd10.  type;
format date yymmdd10.;
datalines;
1 2020-01-01 0
1 2018-07-06 0
2 2015-02-15 1
3 2020-02-01 0
3 2021-07-05 1
3 2021-08-09 0
4 2020-01-30 0
5 2018-10-10 1
5 2019-10-11 1
;
RUN;

proc sql;
create table zero_type as
select * from A where id not in (select id from A where type=1);

create table one_type as
select * from A where id  in (select id from A where type=1);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Dec 2024 07:28:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953798#M372580</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-12-17T07:28:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to output to differing data sets if a value is present across any observations by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953835#M372593</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Will this work with the WHERE= that you have:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; where = (type2 = 1)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or will it require referring to the original variable name?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; where = (type = 1)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Short answer is No.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Drop=/Keep=,Rename=, and Where= dataset options are processed in alphabetical order.&amp;nbsp; So if you have the RENAME= option then there is no longer a variable named TYPE to reference in the WHERE= option.&amp;nbsp; &amp;nbsp;You need to reference it with the new name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if you do not have the RENAME= option so that the original name of TYPE is still used then the values of TYPE in the resulting dataset will be jumbled. However you could fix that by reordering the datasets in the SET statement so that the copy that has all of the observations is listed last, so the real value of TYPE will overwrite the copy that only has 1's.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data onetype zerotype ;
  merge have(in=type1 keep=id type where=(type=1)) have;
  by id;
  if type1 then output onetype;
  else output zerotype;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Dec 2024 13:49:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-to-differing-data-sets-if-a-value-is-present/m-p/953835#M372593</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-12-17T13:49:49Z</dc:date>
    </item>
  </channel>
</rss>

