adding continues 5 days between two given dates in SAS EG - sas

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;

Related

Removing duplicates using several conditions using SAS

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

SAS: Date intervals including 15th of several months

I have difficulties coming up with a nice code for counting the number of times date intervals includes the 15th of the month between 01.01.2019-31.12.2020. As a simple example we can consider the two intervals:
Obs | DateStart | DateEnd
1 14Oct2019 20Mar2020
2 13Nov2018 29Jan2020
I want to determine how many overlaps these have with the 15th of the months between 01Jan2019-31Dec2020. In the end I would like to produce a crosstable showing year in rows and months in columns with a count for each time the above intervals included the 15th of the month. From the above two date intervals, I would like an output like the following:
Months: 1 2 3 4 5 6 7 8 9 10 11 12
2019 1 1 1 1 1 1 1 1 1 2 2 2
2020 2 1 1 . . . . . . . . .
I am currently trying to set up a dataset with columns 1 through 24, which I will then reformat and later cross in a proc freq. This seems like the long way around, and I am having trouble identifying when the date intervals include the 15th of any month.
I have ~100 observations to do this for. Any help would be appreciated.
You can iterate over the date range and output to a second data set (or view) when the date is a 15th. The second data set can be tabulated for frequency counts.
Example:
data have;
call streaminit(2021);
do _n_ = 1 to 1000;
date1 = today() - rand('integer', 2500);
date2 = date1 + rand('integer', 720);
output;
end;
run;
data _15ths(keep=year month);
set have;
do date = date1 to date2;
if day(date) ne 15 then continue;
year = year(date);
month = month(date);
output;
end;
run;
proc tabulate data=_15ths;
title 'Number of date ranges with a 15th';
class year month;
table year='',month*n=''/nocellmerge box='year';
run;
Will produce the following output

converting dates in SAS and creating a new variable

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

Classify records based on a date

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;

SAS - Comparing observations within a group to pick values

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.