<?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 Drop Earliest Date and Add the Latest Date into Data Set? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565344#M158713</link>
    <description>&lt;P&gt;Thank you I also tried to do with following way but it does not work;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table have3 as
select * from have
union all
select * from have2 where YEARMONTH not in(select YEARMONTH from have) ;
quit;

proc sort data=have3;
by ID descending yearmonth;
run;

proc sql noprint;
  select count(distinct(yearmonth)) into :countyear from have3;
quit;
%put &amp;amp;countyear;

data have;
set have3;
by id ;
if not last.id and &amp;amp;countyear.&amp;gt;12;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;any suggestions? &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;@Quentin &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83078"&gt;@SuryaKiran&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Jun 2019 18:18:52 GMT</pubDate>
    <dc:creator>turcay</dc:creator>
    <dc:date>2019-06-11T18:18:52Z</dc:date>
    <item>
      <title>How to Drop Earliest Date and Add the Latest Date into Data Set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/531212#M145354</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to ask you question related to data manipulation. I want to drop the noncurent year and add the current year into my data set (HAVE). Also I want to do this when my data set (HAVE) includes 12 different yearmonth value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually, I made my desired output as below(data set want) but I am not sure whether it is the correct way to do this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ıf somebody has more practical method, does s/he help me, please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
Input  ID YEARMONTH $;
Datalines;
1 201606
1 201607
1 201608
1 201609
1 201610
1 201611
1 201612
1 201701
1 201702
1 201703
1 201704
1 201705
;
Run;
Data Have2;
Input  ID YEARMONTH $;
Datalines;
1 201706
;
Run;

PROC SQL NOPRINT;
    SELECT COUNT(DISTINCT(YEARMONTH)) INTO :COUNTYEAR FROM Have;
QUIT;
%PUT &amp;amp;COUNTYEAR;

%MACRO LOOP;
%IF &amp;amp;COUNTYEAR=12 %THEN %DO;
PROC SQL;
CREATE TABLE HAVE AS
SELECT * FROM HAVE
HAVING YEARMONTH NE MIN(YEARMONTH);
QUIT;
PROC SQL;
CREATE TABLE WANT AS 
SELECT * FROM HAVE
 OUTER UNION CORR 
SELECT * FROM HAVE2
;
Quit;
%END;
%MEND LOOP;
%LOOP;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you,&lt;/P&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;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 23:51:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/531212#M145354</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2019-01-29T23:51:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to Drop Earliest Date and Add the Latest Date into Data Set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/531221#M145361</link>
      <description>&lt;P&gt;That's one way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select count(distinct(YEARMONTH)), min(YEARMONTH) into :countyear,:minyear from HAVE;
quit;

data WANT;
  if &amp;amp;countyear=12 then set HAVE(where=(YEARMONTH ne "&amp;amp;minyear")) HAVE2;
  else set HAVE HAVE2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jan 2019 00:47:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/531221#M145361</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-01-30T00:47:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to Drop Earliest Date and Add the Latest Date into Data Set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565311#M158700</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your response. However, what if my have2 table come 201705 again, I do not want to multiplixity. I want to prevent it. I think we need to add some more controls, right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
Input  ID YEARMONTH $;
Datalines;
1 201606
1 201607
1 201608
1 201609
1 201610
1 201611
1 201612
1 201701
1 201702
1 201703
1 201705
;
Run;
Data Have2;
Input  ID YEARMONTH $;
Datalines;
1 201705
;
Run;

proc sql noprint;
  select count(distinct(YEARMONTH)), min(YEARMONTH),max(YEARMONTH) into :countyear,:minyear, :maxyear from HAVE;
quit;
%PUT &amp;amp;countyear.;
%PUT &amp;amp;minyear.;
%PUT &amp;amp;maxyear.;


data WANT;
  if &amp;amp;countyear=12 then set HAVE(where=(YEARMONTH ne "&amp;amp;minyear")) HAVE2;
  else set HAVE ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 16:02:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565311#M158700</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2019-06-11T16:02:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to Drop Earliest Date and Add the Latest Date into Data Set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565318#M158701</link>
      <description>&lt;P&gt;There's rarely a need to use macros or macro variables to do simple data manipulation like this.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;From what I can see, HAVE has 11 rows , and WANT has 12 rows, where one more month in the sequence is added. So, this code gets you there. (Or perhaps I'm not understanding the problem entirely...)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also make your life much simpler by handling calendar values as actual SAS date values rather than a text string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have end=eof;
	ym=input(yearmonth,yymmn6.);
    output;
    if eof then do;
		ym=intnx('month',ym,1,'b');
		output;
	end;
	format ym yymm6.;
run;
        &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 16:42:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565318#M158701</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-06-11T16:42:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to Drop Earliest Date and Add the Latest Date into Data Set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565322#M158702</link>
      <description>&lt;P&gt;Thank you for your response but I think we both need to control 12 month count and min and max yearmonth control. If there is 12 distinct yearmonth and have data set&amp;nbsp;max(yearmonth) is not equal to Have2 table max(yearmonth)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It should be like following but becaust it is character variable it does not work. Can you help me about it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select count(distinct(YEARMONTH)), min(YEARMONTH),max(YEARMONTH) into :countyear,:minyear, :maxyear from HAVE;
quit;
%PUT &amp;amp;countyear.;
%PUT &amp;amp;minyear.;
proc sql noprint;
  select max(YEARMONTH) into :maxyear from HAVE2;
quit;
%PUT &amp;amp;maxyear.;


data WANT;
  if &amp;amp;countyear=12 and max(YEARMONTH) ne "&amp;amp;maxyear" then set HAVE(where=(YEARMONTH ne "&amp;amp;minyear")) HAVE2;
  else set HAVE ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jun 2019 17:07:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565322#M158702</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2019-06-11T17:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to Drop Earliest Date and Add the Latest Date into Data Set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565339#M158710</link>
      <description>&lt;P&gt;Thanks for the explanation. As far as I can see, there is nothing that requires macros or macro variables here, but as I will not have the time to work on this the rest of today, I will try to come up with something tomorrow.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 17:52:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565339#M158710</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-06-11T17:52:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to Drop Earliest Date and Add the Latest Date into Data Set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565344#M158713</link>
      <description>&lt;P&gt;Thank you I also tried to do with following way but it does not work;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table have3 as
select * from have
union all
select * from have2 where YEARMONTH not in(select YEARMONTH from have) ;
quit;

proc sort data=have3;
by ID descending yearmonth;
run;

proc sql noprint;
  select count(distinct(yearmonth)) into :countyear from have3;
quit;
%put &amp;amp;countyear;

data have;
set have3;
by id ;
if not last.id and &amp;amp;countyear.&amp;gt;12;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;any suggestions? &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;@Quentin &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83078"&gt;@SuryaKiran&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2019 18:18:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565344#M158713</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2019-06-11T18:18:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to Drop Earliest Date and Add the Latest Date into Data Set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565363#M158718</link>
      <description>I think you're seriously over complicating this. Are you trying to ensure that you have a full 12 month for each ID and that it's the current month, not the last month? Please confirm if this is correct and if so, I'll post a revised solution.</description>
      <pubDate>Tue, 11 Jun 2019 19:15:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565363#M158718</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-11T19:15:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to Drop Earliest Date and Add the Latest Date into Data Set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565474#M158777</link>
      <description>&lt;P&gt;If you want my help, please post&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;sufficient example data to illustrate your issue and all edge cases&lt;/LI&gt;
&lt;LI&gt;the expected outcome&lt;/LI&gt;
&lt;LI&gt;the rule(s) for producing that outcome&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Wed, 12 Jun 2019 06:57:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565474#M158777</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-06-12T06:57:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to Drop Earliest Date and Add the Latest Date into Data Set?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565512#M158796</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I think you're seriously over complicating this. Are you trying to ensure that you have a full 12 month for each ID and that it's the current month, not the last month? Please confirm if this is correct and if so, I'll post a revised solution.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; makes a very good point. Is it correct? You haven't given this explanation, and quite honestly, the more I think about what you said, the less clear I am on what you want. The explanation you have given doesn't seem general enough to program, except for the very specific original data set you provided, so its not difficult to provide a solution for that one original data set with 11 observations (and I have done that) but obviously you have a more general problem and we are not sure exactly what that more general problem is.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 11:35:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Drop-Earliest-Date-and-Add-the-Latest-Date-into-Data-Set/m-p/565512#M158796</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-06-12T11:35:39Z</dc:date>
    </item>
  </channel>
</rss>

