<?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 Conditional flagging id by few variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723755#M224653</link>
    <description>&lt;P&gt;&lt;FONT face="andale mono,times" size="4"&gt;Dear All,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="andale mono,times" size="4"&gt;Can I please seek your help on this by using sql?&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="andale mono,times" size="4"&gt;I have a dataset containing id, date_1, date_2 and indication.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="andale mono,times" size="4"&gt;I would like to derive get, keep, date_3 and group.&lt;/FONT&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;FONT face="andale mono,times" size="4"&gt;The rationale is to &lt;/FONT&gt;&lt;FONT face="andale mono,times" size="4"&gt;flag keep=Y for the all the data rows with the earliest dates of&amp;nbsp;both date_1 and date_2 for all the ids&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="andale mono,times" size="4"&gt;If the data rows of keep=Y having both the same month and year i.e. id=1, then date_3=date_2 and group=Gen&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="andale mono,times" size="4"&gt;If the data rows of keep=Y having different month and year i.e. id=2, then group=Indication.&lt;/FONT&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;FONT face="andale mono, times" size="4"&gt;Thanking you in advance.&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT face="andale mono,times" size="4"&gt;id	date_1	        date_2	       indication	keep	date_3	       group
1	01-02-2020	20-02-2020	Y			
1	01-02-2018	03-03-2018	Y			
1	01-02-1990	20-02-1990	N	        Y	       20-02-1990	Gen
1	01-02-1990	20-02-1990	Y	        Y	       20-02-1990	Gen
1	01-02-1990	20-02-1990	Y	        Y	       20-02-1990	Gen
1	01-02-1990	20-02-1990	N	        Y	       20-02-1990	Gen
2	01-06-2009	25-08-2010	Y	        Y		                        Y
2	01-06-2009	25-08-2010	N	        Y		                        N
2	01-06-2009	25-08-2010	N	        Y		                        N
2	01-06-2009	25-08-2010	Y	        Y		                        Y
2	01-05-2018	08-06-2019	Y			
2	01-05-2018	04-12-2019	Y			
&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 05 Mar 2021 03:20:00 GMT</pubDate>
    <dc:creator>Miracle</dc:creator>
    <dc:date>2021-03-05T03:20:00Z</dc:date>
    <item>
      <title>Conditional flagging id by few variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723755#M224653</link>
      <description>&lt;P&gt;&lt;FONT face="andale mono,times" size="4"&gt;Dear All,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="andale mono,times" size="4"&gt;Can I please seek your help on this by using sql?&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="andale mono,times" size="4"&gt;I have a dataset containing id, date_1, date_2 and indication.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="andale mono,times" size="4"&gt;I would like to derive get, keep, date_3 and group.&lt;/FONT&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;FONT face="andale mono,times" size="4"&gt;The rationale is to &lt;/FONT&gt;&lt;FONT face="andale mono,times" size="4"&gt;flag keep=Y for the all the data rows with the earliest dates of&amp;nbsp;both date_1 and date_2 for all the ids&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="andale mono,times" size="4"&gt;If the data rows of keep=Y having both the same month and year i.e. id=1, then date_3=date_2 and group=Gen&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="andale mono,times" size="4"&gt;If the data rows of keep=Y having different month and year i.e. id=2, then group=Indication.&lt;/FONT&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;FONT face="andale mono, times" size="4"&gt;Thanking you in advance.&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT face="andale mono,times" size="4"&gt;id	date_1	        date_2	       indication	keep	date_3	       group
1	01-02-2020	20-02-2020	Y			
1	01-02-2018	03-03-2018	Y			
1	01-02-1990	20-02-1990	N	        Y	       20-02-1990	Gen
1	01-02-1990	20-02-1990	Y	        Y	       20-02-1990	Gen
1	01-02-1990	20-02-1990	Y	        Y	       20-02-1990	Gen
1	01-02-1990	20-02-1990	N	        Y	       20-02-1990	Gen
2	01-06-2009	25-08-2010	Y	        Y		                        Y
2	01-06-2009	25-08-2010	N	        Y		                        N
2	01-06-2009	25-08-2010	N	        Y		                        N
2	01-06-2009	25-08-2010	Y	        Y		                        Y
2	01-05-2018	08-06-2019	Y			
2	01-05-2018	04-12-2019	Y			
&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 03:20:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723755#M224653</guid>
      <dc:creator>Miracle</dc:creator>
      <dc:date>2021-03-05T03:20:00Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional flagging id by few variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723780#M224661</link>
      <description>&lt;P&gt;Please post the data in usable form and explain why you have to use proc sql. Is this homework?&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 06:01:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723780#M224661</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-03-05T06:01:01Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional flagging id by few variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723782#M224663</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp; for your response.&lt;/P&gt;
&lt;P&gt;It is not homework.&lt;/P&gt;
&lt;P&gt;It is a claim data with &amp;gt;1billion data rows.&lt;/P&gt;
&lt;P&gt;I was advised to use sql because it will run faster but I am not good at sql.&lt;BR /&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 06:23:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723782#M224663</guid>
      <dc:creator>Miracle</dc:creator>
      <dc:date>2021-03-05T06:23:03Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional flagging id by few variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723787#M224664</link>
      <description>&lt;P&gt;If the data is stored in a database, i agree: using proc sql with passthrough is a good idea, but if the data is stored in a dataset, i would avoid using proc sql, especially if it is large. But maybe &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt; can share his thoughts about performance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 07:28:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723787#M224664</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-03-05T07:28:39Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional flagging id by few variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723790#M224666</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Yes, you are correct. The data is stored in a database.&lt;BR /&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp; for pointing out.&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 07:41:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723790#M224666</guid>
      <dc:creator>Miracle</dc:creator>
      <dc:date>2021-03-05T07:41:19Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional flagging id by few variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723791#M224667</link>
      <description>&lt;P&gt;Sorry your need is unclear.&lt;/P&gt;
&lt;P&gt;Which date do we consider to assess same month and year?&lt;/P&gt;
&lt;P&gt;What happens is same month but not same year, or same year but not same month?&lt;/P&gt;
&lt;P&gt;This should get you started:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;   
  ROW=_N_;
  infile cards missover;
  input ID DATE_1 ddmmyy10. DATE_2 : ddmmyy10.  INDICATION $ KEEP $;
  format   DATE_1 ddmmyy10. DATE_2 : ddmmyy10.;
cards;
1 01-02-2020 20-02-2020 Y   
1 01-02-2018 03-03-2018 Y   
1 01-02-1990 20-02-1990 N         Y        20-02-1990 
1 01-02-1990 20-02-1990 Y         Y        20-02-1990 
1 01-02-1990 20-02-1990 Y         Y        20-02-1990 
1 01-02-1990 20-02-1990 N         Y        20-02-1990 
2 01-06-2009 25-08-2010 Y         Y                   
2 01-06-2009 25-08-2010 N         Y                   
2 01-06-2009 25-08-2010 N         Y                   
2 01-06-2009 25-08-2010 Y         Y                   
2 01-05-2018 08-06-2019 Y   
2 01-05-2018 04-12-2019 Y   
run;
 
proc sql;    
  create table KEEP_Y as
  select *, intnx('month',min(DATE_1),0) =  intnx('month',min(DATE_2),0) as EQ  
  from HAVE  
  where KEEP='Y'  
  group by ID;
proc sql;
  create table WANT as 
  select h.*, ifc(EQ=1 &amp;amp; h.KEEP='Y','Gen',ifc(EQ=0 &amp;amp; h.KEEP='Y',h.INDICATION,' ')) as GROUP
  from HAVE        h
         left join
       KEEP_Y      k
         on  h.ROW=k.ROW
  order by h.ROW;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="left"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col"&gt;ROW&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;ID&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;DATE_1&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;DATE_2&lt;/TH&gt;
&lt;TH class="l b header" scope="col"&gt;INDICATION&lt;/TH&gt;
&lt;TH class="l b header" scope="col"&gt;KEEP&lt;/TH&gt;
&lt;TH class="l b header" scope="col"&gt;GROUP&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;01/02/2020&lt;/TD&gt;
&lt;TD class="r data"&gt;20/02/2020&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;01/02/2018&lt;/TD&gt;
&lt;TD class="r data"&gt;03/03/2018&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;01/02/1990&lt;/TD&gt;
&lt;TD class="r data"&gt;20/02/1990&lt;/TD&gt;
&lt;TD class="l data"&gt;N&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;Gen&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;01/02/1990&lt;/TD&gt;
&lt;TD class="r data"&gt;20/02/1990&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;Gen&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;01/02/1990&lt;/TD&gt;
&lt;TD class="r data"&gt;20/02/1990&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;Gen&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;01/02/1990&lt;/TD&gt;
&lt;TD class="r data"&gt;20/02/1990&lt;/TD&gt;
&lt;TD class="l data"&gt;N&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;Gen&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;7&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;01/06/2009&lt;/TD&gt;
&lt;TD class="r data"&gt;25/08/2010&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;8&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;01/06/2009&lt;/TD&gt;
&lt;TD class="r data"&gt;25/08/2010&lt;/TD&gt;
&lt;TD class="l data"&gt;N&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;N&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;9&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;01/06/2009&lt;/TD&gt;
&lt;TD class="r data"&gt;25/08/2010&lt;/TD&gt;
&lt;TD class="l data"&gt;N&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;N&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;10&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;01/06/2009&lt;/TD&gt;
&lt;TD class="r data"&gt;25/08/2010&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;11&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;01/05/2018&lt;/TD&gt;
&lt;TD class="r data"&gt;08/06/2019&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;12&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;01/05/2018&lt;/TD&gt;
&lt;TD class="r data"&gt;04/12/2019&lt;/TD&gt;
&lt;TD class="l data"&gt;Y&lt;/TD&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD class="l data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 07:42:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723791#M224667</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-03-05T07:42:19Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional flagging id by few variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723796#M224670</link>
      <description>&lt;P&gt;It is always a good idea to pass the SQL to the remote database.&lt;/P&gt;
&lt;P&gt;The reason is not that the database is faster than SAS (It could be. Or not), but that the data transfer is always very slow, especially for the volumes mentioned.&lt;/P&gt;
&lt;P&gt;The code I provided can be adapted:&lt;/P&gt;
&lt;P&gt;- Write the SQL as pass-through&lt;/P&gt;
&lt;P&gt;- Change the date-comparison test to a syntax appropriate to the database rather than using function INTNX()&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- Change the IFC() function to a CASE clause&lt;/P&gt;
&lt;P&gt;- Combine both statements into a single statement using a subclause&lt;/P&gt;
&lt;P&gt;Merging the original data to the flagged data will also require some other logic as presumably the row number cannot be used.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 07:57:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723796#M224670</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-03-05T07:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional flagging id by few variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723798#M224671</link>
      <description>&lt;P&gt;This query gives the same result as the previous query, and is easier to translate :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select *
       , month(DATE_1)=month(DATE_2) and year(DATE_1)=year(DATE_2) as EQ
       , KEEP='Y' as K
       , case when calculated K and calculated EQ=1 then 'Gen'
              when calculated K and calculated EQ=0 then INDICATION
              else                                       ' '         end as GROUP
  from HAVE        
  group by ID, KEEP
  order by ROW;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 08:21:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-flagging-id-by-few-variables/m-p/723798#M224671</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-03-05T08:21:47Z</dc:date>
    </item>
  </channel>
</rss>

