How to calculate month number in sas - sas

Hi I need to calculate the value of month supposed in sas
01jan1960 is equal to 1
02jan1960 is equal to 2
So I need to calculate for 01aug2020
I used intck function but no output
I want in datastep only .

SAS stores dates as the number of days since 1960 with zero representing first day of 1960. To represent a date in a program just use a quoted string followed by the letter D. The string needs to be something the DATE informat can interpret.
Let's run a little test.
6 data _null_;
7 do dt=0 to 3,"01-JAN-1960"d,'01AUG2020'd;
8 put dt= +1 dt date9.;
9 end;
10 run;
dt=0 01JAN1960
dt=1 02JAN1960
dt=2 03JAN1960
dt=3 04JAN1960
dt=0 01JAN1960
dt=22128 01AUG2020
So the date value for '01AUG2020'd is 22,128.

Subtraction works
days_interval = '01Aug2020'd - '01Jan1960'd;
Or looking at the unformatted value as SAS stores dates from 01Jan1960
days_interval = '01Aug2020'd;
format days_interval 8.;

Related

How to create a new variable of age based upon an existing numeric date born variable in sas?

I want to create a numeric age variable using an existing numeric born date variable (MMDDYY10) in SAS. This "BORN" variable is numeric with a length of 8, the format is MMDDYY10. I'm assuming to use: age=today's date -BORN date. However, BORN date is like:-15226、-8803….I just don't understand why before these number, there is a minus signal. So what is the code to transfer to actual age?
I don't understand why before born date number, there is a minus signal. So how to use today's date minus born date of patient?
SAS is using a number for date/time. Dates are defined as number of days between 1.1. 1960 and specified date, so dates before that time are negative. To translate it to a (for people) readable form, you have to use formats (for example MMDDYY10.)
Similarly time is a number of seconds since midnight of the current day. SAS time values are between 0 and 86400.
Your code would look like this:
data have;
input born MMDDYY10.;
format born MMDDYY10.;
datalines;
03/17/2000
11/11/1988
08/11/1923
;
run;
data want;
set have;
age = floor((DATE()-born) / 365.25);
run;
SAS will correctly translate your input (if you correctly used your formats) into numbers, which are easy for a program to calculate with.

Validating multiple dates to the same hour timepoint

I have a dataset with several subjects with hour timepoints (0.02,24.02,48.02 etc) for each record per subject.
Each record has 4 dates with a single record assigned to each timepoint (0.02= 28AUG2019, 24.02= 29AUG2019 etc).
The date should be the same for each hour timepoint.
What sas function could I use to validate that the dates for each record assigned to each hour timepoint is the same for each subject ?
Would the IFC/IFN function work in this scenario?
Sample data for one subject
For the case of a data set with variables time1-time4 and date1-date4 I see at least two interpretations of the question.
One
Are the date values all the same across the record?
The date values can be arrayed and examined in a loop for a change from one index to the previous.
data have;
format time1-time4 6.2 date1-date4 date9.;
informat date1-date4 date9.;
input time1-time4 date1-date4;
datalines;
0.02 24.02 48.02 72.02 28AUG2019 29AUG2019 28AUG2019 28AUG2019
1.02 34.02 58.02 82.02 01SEP2019 01SEP2019 01SEP2019 01SEP2019
run;
data want;
set have;
array dates date1-date4;
do index=2 to dim(dates);
if dates(index) ne dates(index-1) then at_least_one_date_different_flag = 1;
end;
drop index;
run;
Two
The date should be the same for each hour timepoint.
This could be construed to mean looking down the data set, for each distinct value of time in any of the time1-time4 variables the corresponding date values in the date1-date4 must be also be distinct.
row 1: 1 2 3 4 A B C D
row 2: 1 2 3 5 A B C D
row 3: 1 3 5 2 A X D B
Looking down you have 3:C, 3:C, 3:X which would be different (3 has C's and X's)
If this is the case, please update the question with more sample data of what you have and want.

Excel to SAS Date not working in SAS

I Have a file from excel that is in a short date format, but when SAS reads it in, it turns it into numbers in the 4000 range...when I try and convert this to an excel date with the below formula, it turns the year into 2077...is there a formula to ensure that this date remains in the original format on the read in, or avoid it turning into this 4000 range that is not at all close to the 2017 and 2018 year that my file is starting in. Does that make sense?
data change_date;
format Completed_Date mmddyy8. ;
set check;
completed_date = date_completed;
if 42005 => date_completed >=43466 and date_completed ^=. then
Completed_date = Date_Completed-21916; *commented out 12-21-17 Xalka
dates back to how they are expected;
run;
I am pretty sure this is a duplicate question, but I can't find it.
This is usually caused by mixing character and date values in the same column. This made SAS import the data as a character variable and it results in the actual dates being copied as character versions of the integers that Excel uses to store dates.
Frequently this is caused by entries that look like dates but are really character strings in the Excel file. The best way to fix it is to fix the Excel file so that the column only contains dates. Otherwise you just need to convert the strings to integers and adjust the values to account for the differences in index dates.
So if your values are in a SAS dataset named HAVE in the character variable DATESTRING then you could use this data step to create a new variable with an actual date value.
data want ;
set have ;
if indexc(datestring,'-/') then date=input(datestring,anydtdte32.);
else date = input(datestring,32.) + '01JAN1900'D -2;
format date yymmdd10. ;
run;
The minus 2 is because of difference in whether to start numbering with 1 or 0 and because Excel thinks 1900 was a leap year.
Excel and SAS have different default dates in back-end.
Day 0 in SAS is 1 January 1960 and Day 0 in Excel is 1 January 1900.
So, you will need to convert excel numeric date to sas date using the below formula.
SAS_date = Excel_date - 21916;
data dateExample;
informat dt mmddyy8.;
set dates;
SAS_date = dates - 21916;
dt=sas_Date;
format dt date9.;
run;

Extracting the year from an 8 digit int (ddmmyyyy) in SAS

I have an 8 digit date of birth (e.g., 19860710). How can I extract the just the 4 digits of the year portion and store it in a new variable (e.g., 1986) in SAS?
i suggest when dealing with dates to (nearly) always to convert to SAS dates. You will save a lot of headache that way.
data wanted;
sas_date=input('19860710', yymmdd8.);
Sae_year=year(sas_date);
call symput('Year_mac_var', Sae_year);
run;
%put &Year_mac_var.;
1986
You can do this via Substring Proc sql or by arithmetic ways. This is just what I prefer.
Using substring:
num=19860710; /*Date as Numeric*/
txt=put(num,8.); /*Change Numeric to Char*/
year=substr(txt,1,4);
Or
year=substr("19860710",1,4);
data _null_;
date=19860710;
year=year(input(put(date,8.),yymmdd8.));
put year;
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;