I need to convert SAS character dates without imputing to numeric dates in yymmdd10. format. I tried several formats but it comes out as blank. Is it possible to change character SAS dates to numeric SAS dates?
data check;
length date $10;
date="2013-04-17"; output;
date="2012-11"; output;
date="2011-12-13"; output;
date="2015-03-24"; output;
date="2014"; output;
run;
If I understand "without imputing" correctly, the answer is "you can't".
SAS numeric dates are the number of days from January 1, 1960, until the date you specify. This only has meaning for a specific day: "2014-11" (November 2014) doesn't have a specific number of days from January 1, 1960, for example, there's a 30 day span there, and "2014" is even worse.
When you have only part of the date, you can impute the day, month, or year values (1st, 15th, 30th of the month, or a random day, etc.); but without imputing you cannot have a SAS numeric date value.
Use the INPUT() function to convert a string to a number. SAS stores dates as numbers, so this function can be used for the conversion.
data want;
set check;
format date2 date9.;
date2 = input(date,anydtdte10.);
run;
Here anydtdte10. is an INFORMAT that tells the function how to interpret the string. It is a generic INFORMAT for most date strings.
Note the 2014 string is not going to work with this. You can check for a missing value in date2 and attempt a different conversion.
Related
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.
I need to change what it is currently at into the date9 format.
input Company_Name$ 1-58 Form_Type$ 59-70 CIK Date_filed$ 86-96 File_Name$ 118-141;
length Date_filed $10.;
format Date_filed yymmdd10.;
ndate=put(Date_filed, yymmdd10.);
this is what I have, but it doesn't work. I am not sure how to do this.
SAS stores dates in numeric variables as the number of days since 1960. Once you have a valid date value you can attach any of the many formats that change how the value is displayed. To read directly from the source file into a date value using an INFORMAT that is appropriate for the value. Note that you cannot combine an informat with the column range in the input statement. Instead you can use # to position at the first column of the date string and then use formatted input. Also you did not tell INPUT what columns to read for the variable CIK.
input Company_Name $ 1-58 Form_Type $ 59-70
CIK 71-85 #86 Date_filed yymmdd10.
File_Name $ 118-141
;
format Date_filed date9.;
Note you were reading 11 columns before but date strings in YYYY-MM-DD style only need 10 columns, so check if your date strings are in columns 86-95 or columns 87-96. If you don't read the right columns you might miss the second digit of the day of the month or the first digit of the year.
In Base SAS, how do i convert a character Year (2018) to 01/01/2018 in date format?
data date;
length charyear $8.;
charyear='2018';
run;
In general, when constructing a date from date parts, you should use SAS MDY function. MDY(month,day,year) returns a numeric date, but you need to convert all arguments to numbers, before using the function.
data date;
length charyear $8.;
charyear='2018';
format numDate date9.;
numDate = mdy(1,1, input(charyear, best.));
run;
In your particular case it can be done even easier by using function INPUT with a corresponding date informat:
data date;
length charyear $8.;
charyear='2018';
format numDate date9.;
numDate = input("01JAN" || charyear, date9.);
run;
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;
How do I convert 06JAN2005(as date format) to 200501 (as a number)
Note that there are only two different variable types in SAS - character and numeric. Dates are stored in numeric variables and are generally formatted to appear as user-readable dates. Specifically, dates are stored as the number of days since 01JAN1960.
So if you have a number already formatted as a date, and would like it to appear formatted differently (but still be a number) you simply need to change the format.
As Joe's comment says - I can't see the relationship between the date you provided and the number you desire (maybe you have a typo?). But the below piece of code will demonstrate how you can simply change the format to change the appearance of the number:
data _null_;
format date1 best.
date2 yymmddn6.
date3 date9.
;
date1 = '06JAN2005'd;
date2 = '06JAN2005'd;
date3 = '06JAN2005'd;
put date1= date2= date3=;
run;
Gives:
date1=16442 date2=050106 date3=06JAN2005
If you want an explicit numeric value, you can use the date functions on your date variable which, as pointed out, is a numeric date value SAS understands.
newdatevar=100*year(olddatevar)+month(olddatevar)
Well if you mean you need year||month then something like this should work:
data two;
set one;
order_month = month(date);
order_year = year(date);
order = put(compress(order_month||order_year),6.)-0;
run;
Disclaimer being I'd reccomend just formatting whatever else you have properly, so you don't need to represent the date as a number. If you're looking to add or substract increments of dates, you might want to look at the intx function too
Try this:
data a;
attrib
v_input format=date.
v_output_n format=8.;
v_input = '06JAN2005'd;
v_output_n = put(v_input,yymmn6.);
run;