intck() giving negative value - date

I am new to SAS and I am having trouble with finding the difference between 2 dates.
I have 2 columns: checkin_date and checkout_date
the dates are in mmddyy10. format (mm/dd/yyyy).
I have used the following code:
stay_days= intck('day', checkin_day, checkout_day);
I am getting the right values for dates in the same month but wrong values for days that are across 2 months. For example, the difference between 02/06/2014 and 02/11/2014 is 5. But the difference between 1/31/2014 and 2/13/2014 is -18 which is incorrect.
I have also simply tried to subtract them both:
stay_day = checkout_day - checkin_day;
I am getting the same result for that too.
My entire code:
data hotel;
infile "XXXX\Hotel.dat";
input room_no num_guests checkin_month checkin_day checkin_year checkout_month checkout_day checkout_year internet_used $ days_used room_type $16. room_rate;
checkin_date = mdy(checkin_month,checkin_day,checkin_year);
informat checkin_date mmddyy.;
format checkin_date mmddyy10.;
checkout_date = mdy(checkout_month,checkout_day,checkout_year);
informat checkout_date mmddyy.;
format checkout_date mmddyy10.;
stay_day= intck('day', checkin_day, checkout_day);

Your problem is a typo - using wrong variables in intck() function. You are using variables "xxx_DAY" which is the DAY of month instead of the full DATE. Change to stay_day= intck('day', checkin_date, checkout_date);

Your data probably has the date values in the wrong variables. When using subtraction the order should be ENDDATE - STARTDATE. When using INTNX() function the order should be from STARTDATE to ENDDATE. In either case if the value in the STARTDATE variable is AFTER the value in the ENDDATE variable then the difference will be a negative number.
Perhaps you need to clean the data?
The only way to get -18 comparing 2014-01-31 and 2014-02-13 would be if you extracted the day of the month and subtracted them.
diff3 = day(end) - day(start);
which would be the same as subtracting 31 from 13.
Example using your dates:
data check;
input start end ;
informat start end mmddyy.;
format start end yymmdd10.;
diff1=intck('day',start,end);
diff2=end-start;
cards;
02/06/2014 02/11/2014
1/31/2014 2/13/2014
;
Results:
Obs start end diff1 diff2
1 2014-02-06 2014-02-11 5 5
2 2014-01-31 2014-02-13 13 13

Related

how do i write in sas a filter that subtracts dates

Hello Stackoverflow community... hope you can help with this sas question.
I need to create a filter for a table which gives back only those records that are active from last year forward.
I would like to obtain something like :
data want;
set have;
where expire_date >= current(date) - 1year:
run;
the format of the expire_date column is 03MAY2022 (date9. format)... I tried to transform the date into a number and then subtracting 365, but i guess there is a better solution.
can someone illuminate me?
thanks in advance
I think you are searching for the INTNX() function:
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/p10v3sa3i4kfxfn1sovhi5xzxh8n.htm#n1lchasgjah7ran0z2wlmsbfwdx2
For example:
data a;
format num_date num_date_minus_year DATE9.;
char_date="03MAY2022";
num_date = inputn (char_date, "DATE9.");
num_date_minus_year = intnx ('YEAR', num_date, -1, "SAME");
put num_date= num_date_minus_year=;
run;
Output:
num_date=03MAY2022 num_date_minus_year=03MAY2021
You can get the current date using the DATE() function.
Do you want one YEAR or 365 days?
To use a date interval use the INTNX() function.
where expire_date >= intnx('year',date(),-1,'same') ;
To use a fixed number of days just subtract the number.
where expire_date >= date() - 365 ;

SAS 94 How to calculate the number of days until next record

Using SAS I want to be able to calculate the number of days between two dates where the value is the number of days until the next record.
The required output will be:
Date Num Days
10/09/2020 1
11/09/2020 1
12/09/2020 1
14/09/2020 2
15/09/2020 1
16/09/2020 1
17/09/2020 1
18/09/2020 1
20/09/2020 2
I have tried using Lag and Retain but just cant get it work.
Any advice and suggestions would be really appreciated.
If you sort the data by descending DATE then it is easier because then you just need to look backwards to find the next date. So you can use LAG() or DIF() function.
data want;
set have;
by descending date;
num_days = dif(date);
run;
To simulate a "lead" function you can set another copy of the data skipping the first observation.
data want;
set have ;
set have(firstobs=2 keep=date rename=(date=next_date)) have(obs=1 drop=_all_);
num_days = next_date - date;
run;

Changing the values of Dates in SAS

Complete novice with SAS and I'm trying to convert a yearly range of dates to just "2014", "2015" & "2016." So for example I have an Orders column with a lots of dates in 2014, 2015 and 2016 and want to just convert the values in each year to just the name of the year. The code I was trying to use is below.
Data SortingDates;
set work.ClaraData;
if OrderDate <='31Dec2014'd then OrderDate = "2014";
if '01Jan2015'd <= OrderDate <= '31Dec2015'd then OrderDate= "2015";
if '01Jan2016'd <= OrderDate <= '31Dec2016'd then OrderDate = "2016";
run;
However this message comes: Character values have been converted to numeric values at the places given by...
Plus when printing the data, the dates all come out as 09/07/1965
The OrderDate column is properly formatted as "OrderDate Num 8 DDMMYY10. DDMMYY10."
Thanks!
You are getting the warning because you tried to assign the characters string "2014" to the numeric variable OrderDate. SAS probably successfully converted "2014" into 2,014 for you but since you didn't change the format it should display it as '07/07/1965' since that is the date that is 2,014 days since 01JAN1960.
It is probably easiest if you use the YEAR() function to get the year of a date value.
OrderYear = year(OrderDate);
But you could also just try using the YEAR. format on your existing OrderDate variable.
proc freq data=ClaraData ;
tables OrderDate ;
format OrderDate year. ;
run;
Try the year function (page 15 of this PDF): https://www.sas.com/storefront/aux/en/spfunctionxexample/62857_excerpt.pdf
Data SortingDates;
set work.ClaraData;
OrderDate = YEAR(OrderDate);
run;
Or keeping it as a date, try the year format (like page 8 of the same pdf)
Data SortingDates;
set work.ClaraData;
format OrderDate YEARw.;
run;

SAS date swap year and day

I am working with a dataset containing a date variable with the format MMDDYY10..
The problem is, that the day, month and the year have been swapped.
The data set as it looks now:
Obs Date
1 11/01/1931
2 11/06/1930
3 12/02/2003
4 12/07/2018
What I would like is a date variable with the format DDMMYY10., or a similar:
Obs Date
1 31/01/2011
2 30/06/2011
3 03/02/2012
4 18/07/2012
Observation 1 is hence written as the 1st of November 1931, but really it is the 31st of January 2011.
Does anyone know how I can change this?
Looks like you read the original raw data using the wrong INFORMAT. Most likely you had data in YYMMDD format and you read it as MMDDYY. You can use the PUT() and INPUT() functions to attempt to reverse it.
data have ;
input date mmddyy10.;
newdate = input(put(date,mmddyy6.),yymmdd6.);
format date newdate yymmdd10. ;
put (date newdate) (=);
cards;
11/01/1931
11/06/1930
12/02/2003
12/07/2018
;;;;
Results:
date=1931-11-01 newdate=2011-01-31
date=1930-11-06 newdate=2011-06-30
date=2003-12-02 newdate=2012-02-03
date=2018-12-07 newdate=2012-07-18

removing day portion of date variable for time series SAS

I'm having some frustration with dates in SAS.
I am using proc forecast and am trying make my dates spread evenly. I did some pre-processing wiht proc sql to get my counts by month but my dates are incorrect.
Though my dataset looks good (b/c I used format MONYY.) the actual value of that variable is wrong.
date year month count
Jan10 2010 1 100
Feb10 2010 2 494
...
..
.
The Date value is actually the full SAS representation of the date (18267), meaning that it includes the day count.
Do I need to convert the variable to a string and back to a date or is there a quick proc i can run?
My goal is to use the date variable with proc forecast so I only want Month and year.
Thanks for any help!
You can't define a date variable in SAS (so the number of days passed from 1jan1960) excluding the day.
What you can do is to hide the day with a format like monyy. but the underlying number will always contain that information.
Maybe you can use the interval=month option in proc forecast?
Please add some detail about the problem you're encountering with the forecast procedure.
EDIT: check this example:
data past;
keep date sales;
format date monyy5.;
lu = 0;
n = 25;
do i = -10 to n;
u = .7 * lu + .2 * rannor(1234);
lu = u;
sales = 10 + .10 * i + u;
date = intnx( 'month', '1jul1991'd, i - n );
if i > 0 then output;
end;
run;
proc forecast data=past interval=month lead=10 out=pred;
var sales;
id date;
run;