<?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 ID by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-ID-by-group/m-p/795656#M255236</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm getting different results:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jorgemaiden_0-1644586615704.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68390i46F00E4C772F568D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jorgemaiden_0-1644586615704.png" alt="jorgemaiden_0-1644586615704.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, the "active" subproduct is not always x, could be another.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
    <pubDate>Fri, 11 Feb 2022 13:37:37 GMT</pubDate>
    <dc:creator>jorgemaiden</dc:creator>
    <dc:date>2022-02-11T13:37:37Z</dc:date>
    <item>
      <title>Create ID by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-ID-by-group/m-p/795646#M255230</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to determine the MIN date for a subproduct associated with a product. The subproduct ID can change to another and than return to the original. I need the MIN date for the active sequence of the subproduct .&lt;BR /&gt;An example in the table below - in green the date that I want for each product.&lt;/P&gt;&lt;P&gt;I thought doing this by creating an ID column with a counter and then select the min date for each product when ID = 1.&lt;/P&gt;&lt;P&gt;The problem is that I can't create the ID column the way I need.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jorgemaiden_0-1644583919804.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68389i773885CB33548D30/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jorgemaiden_0-1644583919804.png" alt="jorgemaiden_0-1644583919804.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help on how to create the ID?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jorge&lt;/P&gt;</description>
      <pubDate>Fri, 11 Feb 2022 12:53:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-ID-by-group/m-p/795646#M255230</guid>
      <dc:creator>jorgemaiden</dc:creator>
      <dc:date>2022-02-11T12:53:11Z</dc:date>
    </item>
    <item>
      <title>Re: Create ID by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-ID-by-group/m-p/795647#M255231</link>
      <description>&lt;P&gt;How do you know what SubProduct you want the Min_Date to be associated with?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and what if the there are 2 obs with the same min_date?&lt;/P&gt;</description>
      <pubDate>Fri, 11 Feb 2022 13:00:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-ID-by-group/m-p/795647#M255231</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-02-11T13:00:23Z</dc:date>
    </item>
    <item>
      <title>Re: Create ID by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-ID-by-group/m-p/795651#M255234</link>
      <description>&lt;P&gt;How about&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input product $ sub_product $ date :ddmmyy10.;
format date ddmmyy10.;
datalines;
a x 10/02/2022
a x 05/02/2022
a x 01/02/2022
a y 28/01/2022
a x 20/02/2022
b x 01/02/2022
b x 28/01/2022
b y 10/01/2022
b y 01/01/2022
d x 27/01/2022
d y 20/01/2022
d x 18/01/2022
d x 16/01/2022
d y 10/01/2022
;

data want(drop = d);
   d = '31dec9999'd;

   do until (last.product);
      set have;
	  by product;
	  if sub_product = 'x' and date &amp;lt; d then d = date;
   end;

   do until (last.product);
      set have;
	  by product;
	  flag = date = d;
	  output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;product sub_product date       flag 
a       x           10/02/2022 0 
a       x           05/02/2022 0 
a       x           01/02/2022 1 
a       y           28/01/2022 0 
a       x           20/02/2022 0 
b       x           01/02/2022 0 
b       x           28/01/2022 1 
b       y           10/01/2022 0 
b       y           01/01/2022 0 
d       x           27/01/2022 0 
d       y           20/01/2022 0 
d       x           18/01/2022 0 
d       x           16/01/2022 1 
d       y           10/01/2022 0 &lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Feb 2022 13:24:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-ID-by-group/m-p/795651#M255234</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-02-11T13:24:46Z</dc:date>
    </item>
    <item>
      <title>Re: Create ID by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-ID-by-group/m-p/795652#M255235</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For each Product I want the Min date for the SubProduct that is "active" at the moment of our analysis since it changed to the current Subproduct.&lt;/P&gt;&lt;P&gt;For example, product a had subproduct x, than changed for subproduct y and than returned to subproduct x. Since the last time it has returned to subproduct x, it had several dates (01-02-2022; 05/02/2022; 10/02/2022), I want the Min date since it has returned to subproduct x (01-02-2022).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2 observations with the same min date doesn't happen in this case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jorge&lt;/P&gt;</description>
      <pubDate>Fri, 11 Feb 2022 13:24:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-ID-by-group/m-p/795652#M255235</guid>
      <dc:creator>jorgemaiden</dc:creator>
      <dc:date>2022-02-11T13:24:34Z</dc:date>
    </item>
    <item>
      <title>Re: Create ID by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-ID-by-group/m-p/795656#M255236</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm getting different results:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jorgemaiden_0-1644586615704.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/68390i46F00E4C772F568D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jorgemaiden_0-1644586615704.png" alt="jorgemaiden_0-1644586615704.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, the "active" subproduct is not always x, could be another.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Feb 2022 13:37:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-ID-by-group/m-p/795656#M255236</guid>
      <dc:creator>jorgemaiden</dc:creator>
      <dc:date>2022-02-11T13:37:37Z</dc:date>
    </item>
    <item>
      <title>Re: Create ID by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-ID-by-group/m-p/795735#M255257</link>
      <description>&lt;P&gt;Found a solution to this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;PROC SQL;

	CREATE TABLE WORK.MAX_DATE AS 

	SELECT product, 
		subproduct, 
		MAX(date) FORMAT = DDMMYY10. AS date
	FROM WORK.data
	GROUP BY 1, 2 
	ORDER BY 1, 3
;
QUIT;


PROC SORT DATA=WORK.MAX_DATE;
BY product descending date;
RUN;

data ID;
	set WORK.MAX_DATE;
	by product NOTSORTED;
	retain id;
	if first.product then id = 1;
	else id+1;
run;

/* Select MIN Date since last subproduct change */
PROC SQL;

	CREATE TABLE WORK.MIN_DATE AS 

	SELECT a.product, 
		a.subproduct, 
		MIN(a.date) FORMAT = DDMMYY10. as date
	FROM WORK.data
		INNER JOIN (SELECT * FROM ID WHERE ID = 2) b ON a.product = b.product AND a.date &amp;gt; b.date
	GROUP BY 1, 2
	ORDER BY 1, 3
;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;Jorge&lt;/P&gt;</description>
      <pubDate>Fri, 11 Feb 2022 18:12:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-ID-by-group/m-p/795735#M255257</guid>
      <dc:creator>jorgemaiden</dc:creator>
      <dc:date>2022-02-11T18:12:06Z</dc:date>
    </item>
  </channel>
</rss>

