SAS Get week number for each month - sas

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;

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 function to remove data if the month matches between 2 dates in 2 set of columns

I have a large data with dates on 2 columns (15 years data). I am looking to remove the date if the month between the 2 columns match (dont care about the day or the year) EX: 01FEB2006 - 01FEB2017 now i need to remove this date as the month matches while 16FEB2006 - 23JUN2017 doesn't therefore i need a new column to say the difference is 4 months. If the difference is +/- 30 days between the 2 months, this have to be removed Ex: 01jan2005 - 20dec2014.
Please help! I am still learning SAS.
Thanks in advance for your help.
Okay. The question is a bit unclear. But as I've deduced the question has number of potential questions:
"Delete obeservations that have matching months"
This may be obtained using month function. (Or if your date is string then substring.) http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000197966.htm
Data refined;
set begin;
month1=month(date_var1 );
month2=month(date_var2 );
if month1= month2 then delete;
run;
"I need a new column to say the difference is 4 months."
intck if your friendly function here. You can calculate the timedifferences between the dates. http://www.sascommunity.org/wiki/INTCK_function_examples
Data refined;
set begin;
TimeDifference = intck('month', date_var1, date_var2);
run;
"if datedifference is +-30 days then delete the observation"
Same as above, but with days in this case.
Data refined;
set begin;
TimeDifference = intck('days', date_var1, date_var2) ;
if abs(TimeDifference) <=30 delete;
run;

Date Function SAS

Using 'intck' for calculating no of days between two dates.
data test;
x=intck('day','01dec2016'd,'31dec2016'd);
run;
Output is x=30.
It should be 31.How to solve it ?`
Here's one way!
data test;
x=intck('day','01dec2016'd,'31dec2016'd)+1;
run;
as #jonrsharpe comments.. There are indeed 30 days between those dates, so if you are looking for the number of days in a month, you can try starting from the last day of the previous month - or simply add one.

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?

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;