Date set having id and date .I want a date set with two duplicate id but condition is that one should be before 8th June and other should be after 8th June.
To take the first date and the first date after 2021-06-08 you can sort by ID and DATE and use LAG() to detect when you cross the date boundary.
data have ;
input id date :date. ;
format date date9.;
cards;
1 01jun2021
1 07jun2021
1 08jun2021
1 09jun2021
;
data want;
set have ;
by id date;
if first.id or ( (date<='08JUN2021'd) ne lag(date<='08JUN2021'd));
run;
results
Obs id date
1 1 01JUN2021
2 1 09JUN2021
Related
What am I trying to do is the following:
Have this table:
Table1
Item Date1 Date 2
1 6/1/2021 7/31/2021
2 7/4/2021 7/30/2021
3 6/20/2021 7/28/2021
....
My want table is the following:
Item Date
1 6/1/2021
1 6/6/2021
1 6/11/2021
1 6/16/2021
...
Basically I am trying to create a date by incrementing 5 days from the start date until the last date.
Something like this should get you started:
data want;
set have;
format date date1 date2 date9.;
do Date=date1 to date2 by 5;
Date = MIN(Date, Date2);
output;
end;
*keep Item Date;
run;
Lets say I have the following dates for the observations
data dates;
input obs date$11.;
cards;
1 06/10/1949
2 01/07/1952
3 02/10/1947
;
run;
But now I want to insert another column next to date called new date under the date9. format and this new date column is to be numeric.
I tried the following,
data newdata;
set dates;
newdate=input(date,date9.);
run;
But when I run this, the new date column seems to be empty
Your string values are not using a format that is compatible with the DATE. informat. They appear to be using either MMDDYY. or DDMMYY., but it is not possible to tell which from your example values.
data dates;
input obs datestr :$11.;
date1 = input(datestr,mmddyy10.);
date2 = input(datestr,ddmmyy10.);
format date1 date2 date9. ;
cards;
1 06/10/1949
2 01/07/1952
3 02/10/1947
;
results:
Obs obs datestr date1 date2
1 1 06/10/1949 10JUN1949 06OCT1949
2 2 01/07/1952 07JAN1952 01JUL1952
3 3 02/10/1947 10FEB1947 02OCT1947
I have the following dataset:
DATA survey;
informat order_date date9. ;
INPUT id order_date ;
DATALINES;
1 11SEPT20016
2 12AUG2016
3 14JAN2016
;
RUN;
PROC PRINT data = survey;
format order_date date9.;
RUN;
What I would like to do now is classify the records based on their last visit. So what I want to do is:
Set a date (fe, 10SEPT 2016)
Classify all records that have a lastvisit > 30days as 1, Classify all records that have a lastvisit > 60days as 2 etc...
Any thoughts on how I need to program this?
You could build something like this (count the days between the dates, divide them by 30 and ceil them). Alternativly, if you want to use months and not 30 days, you can replace the first intck parameter with 'month' and remove the ceil and /30:
DATA survey;
informat order_date date9. ;
INPUT id order_date ;
DATALINES;
1 11SEP2016
2 12AUG2016
3 14JAN2016
4 09SEP2016
5 10AUG2016
;
RUN;
%let lastvisit=10SEP2016;
data result;
set survey;
days_30=ceil(intck('days', order_date,"&lastvisit"d)/30)-1;
run;
PROC PRINT data = result;
format order_date date9.;
RUN;
I have 4 columns in my SAS dataset as shown in first image below. I need to compare the dates of consecutive rows by ID. For each ID, if Date2 occurs before the next row's Date1 for the same ID, then keep the Bill amount. If Date2 occurs after the Date1 of the next row, delete the bill amount. So for each ID, only keep the bill where the Date2 is less than the next rows Date1. I have placed what the result set should look like at the bottom.
Result set should look like
You'll want to create a new variable that moves the next row's DATE1 up one row to make the comparison. Assuming your date variables are in a date format, use PROC EXPAND and make the comparison ensuring that you're not comparing the last value which will have a missing LEAD value:
DATA TEST;
INPUT ID: $3. DATE1: MMDDYY10. DATE2: MMDDYY10. BILL: 8.;
FORMAT DATE1 DATE2 MMDDYY10.;
DATALINES;
AA 07/23/2015 07/31/2015 34
AA 07/30/2015 08/10/2015 50
AA 08/12/2015 08/15/2015 18
BB 07/23/2015 07/24/2015 20
BB 07/30/2015 08/08/2015 20
BB 08/06/2015 08/08/2015 20
;
RUN;
PROC EXPAND DATA = TEST OUT=TEST1 METHOD=NONE;
BY ID;
CONVERT DATE1 = DATE1_LEAD / TRANSFORMOUT=(LEAD 1);
RUN;
DATA TEST2; SET TEST1;
IF DATE1_LEAD NE . AND DATE2 GT DATE1_LEAD THEN BILL=.;
RUN;
If you sort your data so that you are looking to the previous obs to compare your dates, you can use a the LAG Function in a DATA STEP.
I have a data in the given way below
ID typ date
1 A 2014jan01
1 B 2014mar01
1 B 2014apr01
1 A 2014jun01
I want to create a new variable with Count, wrt the typ and also date.
DESIRED OUTPUT
ID typ date count
1 A 2014jan01 1
1 B 2014mar01 1
1 B 2014apr01 2
1 A 2014jun01 1
i wrote this program
proc sort data=have; by ID date typ;run;
data want;
set have;
by ID date typ;
if first.typ then Count=1;
else
Count+1; run;
but it is not giving the desired result.
#Quentin has provided the correct answer (using the NOTSORTED option in the data step). You need to understand how the FIRST variable works with the order of variables in the BY statement. Your order is ID DATE TYP, so FIRST.ID is set to 1 for the first record only, FIRST.DATE is set to 1 for all records as the date is different each time, which means that any subsequent variable (i.e. FIRST.TYP) is also set to 1 for all records. Below is the code you should be running (credit to #Quentin)
proc sort data=have; by ID date typ;run;
data want;
set have;
by ID typ date notsorted;
if first.typ then Count=1;
else Count+1;
run;