Dates and intervals in SAS - sas

If I have two dates e.g. 22MAR2005 to 01MAR2006 and I want to create season intervals (spring, summer, autumn, winter) based on this interval, how can this be done in a data step?
Season's are defined as:
Spring: March to May
Summer: June to August
Autumn: September to November
Winter: December to February
I need to calculate how long they spent in each season.

You need to convert that to MONTH first and then to SEASON. This exact question was asked recently so it's relatively easy to find via search (I'm assuming some course is using this as homework?).
data want;
set have;
*create month;
month_date = month(date);
*assign to season;
if month_date in (6, 7, 8) then season = 'Summer';
else if month_date in (9, 10, 11) then season ='Fall';
....etc;
run;
You could also use a format but since you're just starting out this is likely easier.
Other users question which seems really really similar:
https://communities.sas.com/t5/SAS-Enterprise-Guide/proc-glm/m-p/492142
EDIT: Use INTCK() to calculate the number of intervals.
Then use INTNX to increment across the intervals and count your days.
How you align your dates can be controlled with the first parameter to the INTNX() function. You can check the documentation for the exact specifics.
data want;
start_date="22Mar2005"d;
end_date="01Mar2006"d;
num_intervals=intck('quarter', start_date, end_date, 'C') ;
do interval=0 to num_intervals;
season_start=intnx('Month3.3', start_date, interval, 'b');
season_end=intnx('Month3.3', start_date, interval, 'e');
Number_Days=season_end - season_start + 1;
output;
end;
format start_date end_date season: yymmddd10.;
run;

Related

Quickest way to fill in missing dates in a sequence? SAS

Say you download data for stocks or bonds, you have the stock price or yield for every trading day. So you have two variables, stock price (or yield if bond) and date. What is the quickest way to add weekends and holidays to the dates variable while using the previous open day as the values for those missing days?
For example, if it were July 1, 2022 there would be a stock price, lets say $100, corresponding to that date, but during the long weekend (4th of July) there are no observations in the data with the date being July 2nd through 4th. How do you add those dates with the stock price equaling $100 until the next trading day, July 5th?
I used a do loop to create the dates then merged and retain, but I feel like theres got to be a quicker method
You could just add an OUTPUT statement in a DO loop. The tricky part is getting the next date. Here is a method using a second SET statement that is offset by one observation.
data want;
set have ;
by date;
set have(firstobs=2 keep=date rename=(date=next_date)) have(obs=0 drop=_all_);
next_date = coalesce(next_date,date);
do date=date to next_date;
output;
end;
run;
But your real data probably has multiple stocks. So add some BY group processing.
data want;
set have ;
by stock date;
set have(firstobs=2 keep=date rename=(date=next_date)) have(obs=0 drop=_all_);
if last.stock the next_date=date;
do date=date to next_date;
output;
end;
run;

Sas Macros to get schedule the program to run bi-weekly

I have a proc sql code that needs to run twice each month- 1st and the 16th.
There is a where clause in the proc sql.
When the report runs on say January 1st 2022, the where clause filters records that lie between 16December 2021 to 31st December 2021.
And when the report runs on 16th January 2022, the where clause filters records that lie between 01 January 2022 to 15 January 2022.
I have been manually updating these filters everytime I run it but now I need to automate it. There should only be one schedule, which checks for the report run date and accordingly sets the where clause.
To automate date selection, add the following code to your macro. This will create two start/end-date macro variables that hold dates depending on the current day of month.
data _null_;
/* Beginning, end, 15th, and 16th days of this month and last month */
this_month_b = intnx('month', today(), 0, 'B');
last_month_e = intnx('month', today(), -1, 'E');
this_month_15 = mdy(month(this_month_b), 15, year(this_month_b) );
last_month_16 = mdy(month(last_month_e), 16, year(last_month_e) );
/* Assign start/end dates to macro variables based on the current day of month */
if(day(today() ) < 16) then do;
call symputx('start_date', put(last_month_16, date9.) );
call symputx('end_date', put(last_month_e, date9.) );
end;
else do;
call symputx('start_date', put(this_month_b, date9.) );
call symputx('end_date', put(this_month_15, date9.) );
end;
run;
For example, running it today on Jan 3rd:
%put &start_date;
%put &end_date;
16DEC2021
31DEC2021
Add these macro variables to your SQL where statement.
where date BETWEEN "&start_date"d AND "&end_date"d
As for scheduling, there are numerous ways you can schedule SAS processes, whether it's through cron, Viya Jobs or another schedule manager. There are a lot of papers out there on how to schedule SAS jobs in batch. How you do that is up to you, but the above code will handle dynamically selecting data when it runs.

Counting working days in SAS EG

Hi to all and good time of a day!
Here is my case I need to solve I will very gratefull if you can help me.
I have some data set it contains only one variable date format.
Example:
01JAN2016
06JAN2016
15FEB2016
The second data set is days - holidays for a period 5 years.
Example:
01JAN2016
02JAN2016
and etc, all these days are not working days.
The case is I need to count number of working days from date for every observation from first data set till now. It seems that I need to count number of days
"Now date" minus Date(from first data set) and minus number of days from second data set with holidays (count(date) where Date(from first data set)< date < "Now"
You can define your own type of interval to use with SAS funcions intck and intnx. Here's how to do it:
First create a table of weekdays for whichever years you have holidays for, up to present (or a future) year.
Here we'll start by including all weekdays from 2014 to 2016. This is assuming you don't want to count weekend days. If that's not the case, just modify the code so that the condition "weekday(date) in (2:6)" is not applied. You'll get the full 365 days of the year.
data mon_fri;
do date = "01JAN2014"d to "31DEC2016"d;
if weekday(date) in (2:6) then output;
end;
format date date9.;
run;
Then we'll create a table having all those dates we just created, minus the holidays we have in the table Holidays. We'll place the table in a library called myLib, and rename the date column to "Begin" for compliance with SAS custom intervals.
libname myLib "some/place/on/your/drive";
data mylib.workdays(RENAME=(date=Begin));
merge mon_fri (in=weekday)
Holidays (in=holiday);
by date;
if weekday and not holiday then output;
run;
Now we set up a custom interval which we'll simply call "workdays".
options intervalds=(workdays=mylib.workdays);
From there, all you have left to do is something like this:
data dateCalculations;
set mydata;
numOfDays = intck("workdays", theDate, today());
run;
SAS will take care of counting the number of dates (lines in the workdays dataset) separating the startdate (column called theDate) from the enddate (today's date).
Et voilĂ !
This is wonderful and very helpful. I use two different SAS systems (both on remote Unix servers). Setting the intervalds option only seems to work on one of them. I copy/paste the same code and on the other nothing happens - no warning, no error, it simply doesn't work.
Here is how I'm setting it (download the CSV from Yahoo! Finance for the S&P500, daily data, starting January 1950):
PROC IMPORT DATAFILE="sp500_1950_2016.csv"
OUT=sp500_1950_2016
DBMS=DLM
REPLACE;
delimiter=',';
getnames=yes;
RUN;
data trading_days;
set sp500_1950_2016 (keep = date rename=(date=begin));
where year(begin) < 2017;
run;
options intervalds=(TradingDay=trading_days) ;
Then I call it like so to count number of observations I should have from fund inception to Dec 31, 2016 or when the fund closed, whichever is sooner:
data ops2; set operations_master; where ~missing(inception);
if missing(enddate) then enddate = '31dec2016'd;
datadays = INTCK('TradingDay',inception,enddate);run;
proc univariate; var datadays;run;quit;
On system 1, this works just fine. On system 2, I get 0 for the variable datadays. I've already checked to see if there is a sys admin override on setting the intervalds option, and there is not. Is there another reason why this might not work on a given system?

SAS Get week number for each month

I'm new to SAS and have been trying to figure out how to get the week number for each month. I'm having an issue with the months where they don't start at the beginning of the week. For example, if I have a month where the data from the 1st of the month falls on a Thursday, it shows the 1st and 2nd of that month as week 0. Is there a way to display those weeks as week 1? I've tried different things and have been unsuccessful.
DATA getweek;
set test;
if year(rundt) ne year(today())then delete;
month = month(rundt);
week1=intck('week',intnx('month',rundt,0),rundt);
format rundt MMDDYY8.;
RUN;
This depends largely on how you want to define week number. If the first of the month falls on a wednesday, what week is that week? Is that week one, or week zero? It can be commonly referred to either way.
If you want one, so Thurs/Fri/Sat are week 1, then the 4th is week 1, then just add one to the result - it's consistently going to be off (down) by one, after all.
data test;
do rundt=19805 to 19904;
day_of_month=day(rundt);
output;
end;
format rundt date9.;
run;
DATA getweek;
set test;
if year(rundt) ne year(today())then delete;
month = month(rundt);
week1=intck('week',intnx('month',rundt,0),rundt)+1;
format rundt MMDDYY8.;
RUN;
Now, if you want the first 9 or 10 days to be week 1, you have a different solution (though I don't recommend this). Use min to set it to minimum of one. This means your first week could be as many as 13 days, which is counterintuitive, but if it's what you need, this is how you do it.
DATA getweek;
set test;
if year(rundt) ne year(today())then delete;
month = month(rundt);
week1=min(intck('week',intnx('month',rundt,0),rundt),1);
format rundt MMDDYY8.;
RUN;

week function giving strange result

using the week function to clean some data and eventually will order the weeks. I used week() on the date 8/26/2011 and I got 34, and when the function inserted the date 01/13/2012 it spit out 2. I thouhgt I was getting number of weeks since jan 1, 1960?
As per the WEEK Function documentation, the default U descriptor specifies the number of the week within the year, with Sunday being deemed the 1st day of the week. (You can use V if you want Monday to be considered the 1st day instead.)
The week function calculates the week of the current year. The answer to the implied question, "how do I calculated the number of days since 1/1/1960 [or some arbitrary date]," is the intck function.
data have;
input datevar date9.;
datalines;
01JAN1960
02JAN2013
13JAN2012
26AUG2011
;;;;
run;
data want;
set have;
wks = intck('week',0,datevar); *# of weeks from 0 to datevar [0=1/1/1960].
*Can replace 0 with any other date variable.;
run;