BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
West26
Obsidian | Level 7

Hello,

Need help with below

DATE1         DATE2     DATE3
20-Apr-21   1-Jan-21  31-Dec-99
20-Apr-21   1-Jan-21  31-Dec-99
20-Apr-21   1-Jan-21  31-Jan-00
20-Apr-21   1-Jan-21  31-Dec-99
20-Apr-21   1-Jul-19   31-Dec-20
20-Apr-21   1-Jul-19   31-Dec-20
27-Apr-21   1-Jul-19   31-Dec-20
28-Apr-21   1-Jul-19   31-Dec-20
29-Apr-21   1-Jul-19   31-Dec-20

All the three dates were sorted
Scenario-1)
If DATE1 is not changed, DAT2 is not changed and only DATE3 has changed, I need those rows
That means first two rows
Scenario 2)
Suppose DATE3 doesn't exists
Now I need to get only first 4 rows of DATE1 and DATE2, as there is a change starting in DATE2, in row5

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Scenario 2)

 

data have;
input (DATE1         DATE2     ) (:date11.);
format date: date9.;
cards;
20-Apr-21   1-Jan-21  31-Dec-99
20-Apr-21   1-Jan-21  31-Dec-99
20-Apr-21   1-Jan-21  31-Jan-00
20-Apr-21   1-Jan-21  31-Dec-99
20-Apr-21   1-Jul-19   31-Dec-20
20-Apr-21   1-Jul-19   31-Dec-20
27-Apr-21   1-Jul-19   31-Dec-20
28-Apr-21   1-Jul-19   31-Dec-20
29-Apr-21   1-Jul-19   31-Dec-20
;
data temp;
  set have;
  by date1 date2  notsorted;
  if first.date1 then group=0;
  group+first.date2;
run;
proc sql;
create table want as
select *
 from temp
  group by date1
   having count(distinct group)>1 and group=1;
quit;

View solution in original post

5 REPLIES 5
mkeintz
PROC Star
  1. Please provide your initial data in the form of a working data step.  Save us some time by doing this task.

  2. Please provide your expected data as a result of the rule.  I ask this because you specify (bold italics mine

    Scenario-1)
    If DATE1 is not changed, DAT2 is not changed and only DATE3 has changed, I need those rows
    That means first two rows

    The first two rows are constant for all 3 dates, so how is it that you want the first two rows given the rule you state ("only DATE3 has changes)?

    Do you really mean you want all the rows preceding a change in date3?

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
West26
Obsidian | Level 7

Thanks for your time and the suggestion. Appreciated.

 

data want;
input custid date1 :yymmdd10. date2 :yymmdd10. date3 :yymmdd10.
format date1 yymmdd10. date2 yymmdd10. date3 yymmdd10.;
datalines;
1 20/04/2021 01/01/2021 31/12/1999
2 20/04/2021 01/01/2021 31/12/1999
3 20/04/2021 01/01/2021 31/01/2000
4 20/04/2021 01/01/2021 31/12/1999
5 20/04/2021 01/07/2021 31/12/2000
6 20/04/2021 01/07/2021 31/12/2000
7 27/04/2021 01/07/2021 31/12/2000
8 28/04/2021 01/07/2021 31/12/2000
9 29/04/2021 01/07/2021 31/12/2000

;
run;

Scenario:
1) I want all the rows, preceding a change in date3, like below
Output:
1 20/04/2021 01/01/2021 31/12/1999
2 20/04/2021 01/01/2021 31/12/1999

2) Suppose DATE3 doesn't exists
I want all the rows, preceding a change in DATE2, like below
Output:
1 20/04/2021 01/01/2021
2 20/04/2021 01/01/2021
3 20/04/2021 01/01/2021
4 20/04/2021 01/01/2021

 

Thanks.

Ksharp
Super User

Scenario-1)

 

data have;
input (DATE1         DATE2     DATE3) (:date11.);
format date: date9.;
cards;
20-Apr-21   1-Jan-21  31-Dec-99
20-Apr-21   1-Jan-21  31-Dec-99
20-Apr-21   1-Jan-21  31-Jan-00
20-Apr-21   1-Jan-21  31-Dec-99
20-Apr-21   1-Jul-19   31-Dec-20
20-Apr-21   1-Jul-19   31-Dec-20
27-Apr-21   1-Jul-19   31-Dec-20
28-Apr-21   1-Jul-19   31-Dec-20
29-Apr-21   1-Jul-19   31-Dec-20
;
data temp;
  set have;
  by date1 date2 date3 notsorted;
  if first.date2 then group=0;
  group+first.date3;
run;
proc sql;
create table want as
select *
 from temp
  group by date1,date2
   having count(distinct group)>1 and group=1;
quit;
West26
Obsidian | Level 7
Hi Ksharp,
I'm unable to see anything here.
Ksharp
Super User

Scenario 2)

 

data have;
input (DATE1         DATE2     ) (:date11.);
format date: date9.;
cards;
20-Apr-21   1-Jan-21  31-Dec-99
20-Apr-21   1-Jan-21  31-Dec-99
20-Apr-21   1-Jan-21  31-Jan-00
20-Apr-21   1-Jan-21  31-Dec-99
20-Apr-21   1-Jul-19   31-Dec-20
20-Apr-21   1-Jul-19   31-Dec-20
27-Apr-21   1-Jul-19   31-Dec-20
28-Apr-21   1-Jul-19   31-Dec-20
29-Apr-21   1-Jul-19   31-Dec-20
;
data temp;
  set have;
  by date1 date2  notsorted;
  if first.date1 then group=0;
  group+first.date2;
run;
proc sql;
create table want as
select *
 from temp
  group by date1
   having count(distinct group)>1 and group=1;
quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 480 views
  • 0 likes
  • 3 in conversation