I was wondering if someone could help me:
I have a date formatted as ddmmyyP10. From this date I want to substract a certain number of years (yrs). The date is the same for each observation, only yrs changes.
Example for my goal: date = 01.05.2018, yrs=3 -> resulting in 05.2015 (after formatting accordingly)
I have checked for yrdif, datdif and intnx but none of them seems to fit my data.
My (likely naive) first idea was to perform a simple substraction as I understood that sas stores dates internally as sas dates.
When I did
newdate=date-yrs;
format newdate monyy7.;
it showed for each observation May2018, so basically nothing happened.
Thanks in advance for your help!
Best wishes
Use the INTNX() function and proper formatting.
data have;
input date :ddmmyy10.;
format date ddmmyy10.;
datalines;
01.05.2018
;
data want;
set have;
newdate = intnx('Year', date, -3, 's');
format newdate mmyyp8.;
run;
newdate
05.2015
Related
How can I display the internal date value of a date variable in sas?
I have it currently formatted as a date in the format ddmmyy10. and I would like to display the internal date value.
I initially thought of using the datediff function to get the difference from my date and January 1st 1960 but was wondering if there were a simpler way.
Thanks in advance
Alex
Simply set it as the numeric format 8.
/* Example data */
data have;
date = '02MAY2022'd;
format date date9.;
run;
/* Change the format of date in the dataset 'have' */
proc datasets lib=work nolist;
modify have;
format date 8.;
quit;
Output:
date
22767
Or, in Enterprise Guide, change the format through the GUI:
Just use a different format. Since recent dates are in the tens of thousands of days since 1/1/1960 the COMMA format would work well.
proc print data=have ;
format date comma8. ;
run;
Or remove the format completely and let SAS use its default display method for the numbers.
proc print data=have ;
format date ;
run;
I wanted to know, how can we define date format from given date
for example, i have date 20180423 then in sas I want to define format as 'yyyymmdd'
similarly , i have date given in data as 12022018 then i want to define as 'ddmmyyyy'
Please note that, date is provided to me in proper date, but i want to define format now.
Date given may be different in future
so I need to take care all of the date format through SAS
What I thought was given date 20180422
use substr function
data test;
a=20180422;
a=substr(a,1,4);
b=substr(a,5,1);
c=substr(a,7,1);
run;
but not sure.
If anyone can provide the solution,then it really helps me in my project work.
Thanks in Advance for help.
It sounds like you want to convert various values to a date. SAS stores dates as a number, being the number of days since 1st Jan 1960. It's then usual to format this number to display as a date, in whichever format is preferred.
When importing dates that's are already in a format, it is necessary to use the input function, along with an informat, to convert the formatted value to a SAS date. If the date values being read in are all in the same format, then the specific informat can be used. In your case, where different formats are used, you can use the anydtdte. informat which will convert most of the standard date formats to a SAS date.
The example below converts 3 different date formats to a SAS date, then displays the SAS date in the date9. format. I've printed both the unformatted and formatted new values to the log, just so you can see they are stored as numbers.
data _null_;
input date_in $20.;
date_out = input(date_in, anydtdte20.);
put date_in date_out date_out :date9.;
datalines;
20180422
12022018
27apr2018
;
run;
Use the input(a,anydtdte20.); this will convert any date to SAS date, then use the functions Year(), Month(), Day() to extract the data you want.
You will find this SAS Post very useful about dates and locales.
Solution:
I created a table with two rows; each row have a different date format YYYYMMDD & DDMMYYYY to show you how the code will handles different date formats, saved them to SAS date and broke them down to Year, Month & Day:
options DATESTYLE=DMY;
data have;
input a;
datalines;
20180422
12022018
;
run;
data test;
set have;
format date_a date9.;
date_a=input(a,anydtdte20.);
Year_a=year(date_a);
month_a=month(date_a);
day_a=day(date_a);
run;
Output:
a=20180422 date_a=22APR2018 Year_a=2018 month_a=4 day_a=22
a=12022018 date_a=12FEB2018 Year_a=2018 month_a=2 day_a=12
You can use an if condition inside a data step. Using If condition, check for the condition to be true (check date value satisfies the required criteria), then format the date using a put function.Put function can take a source as first argument and format as second argument , and return the formatted value. Different values of same column, can have different formats specified that way.
Something like this,
if a = 'date1CheckCondtion' then newA = put(a , dateformat1.);
if a = 'date2' then newA = put(a , dateformat2.);
You may then choose to get all values in a common format like this:
dateA=input(newA,mmddyy6.);
My dataset has a date in NUM format 201011 which is Nov 2010. I want it converted to 2010Q4 in date format. I applied YYQ6. format but it shows results as 2510Q4. What is wrong in here?
data abc;
date=201011;
run;
data abc2;
set abc1;
format date YYQ.;
run;
You need to write your date as a date literal, otherwise SAS will interpret it as the number of days since 1st January 1960.
Try this:
data abc;
date='01nov2010'd;
run;
data abc2;
set abc1;
format date YYQ.;
run;
If you have an existing numeric variable in the format yyyymm, you will need to create a new one first before applying the format, e.g.
newdate = input(put(date,6.), yymmn6.);
format newdate yyq.;
data _null_;
datenum=201011;
dateval=mdy(mod(datenum,100),1,floor(datenum/100));
put dateval mmddyy10.;
run;
Not sure the FLOOR function is necessary--in this case, it worked okay without it.
The question is, is this arithmetic manipulation and use of MDY more or less efficient than converting to character and then back to a date?
I have a date that is stored as a number that is 201401. I would like to subtract 1 month, so that is 201312 and not 201400.
Also, if there is a more efficient way, please suggest as well. I also have the date stored as 01Jan2014, and would be fine converting the SAS date at that point, so that I can create two new columns (with 1 month subtracted) so that they have the value 01Dec2013 and 201312. Also, a function for incrementing the month forward or backward would be much appreciated.
Thanks in advance.
If you store the date as a SAS date, you can use the function intnx to increment it by whatever period you like.
newdate = intnx('Month',olddate,1,'s');
If you store it as an integer like your original, you're on your own to figure that out. My answer: don't.
The prior answer works.
I just wanted to add, storing as Date in SAS is not the same as storing as integer. It may display 01JAN2014 but it represents a number, so you can still perform computations.
If you store the date as a SAS date, you can use the function intnx to increment it by whatever period you like.
newdate = intnx('Month',olddate,1,'s');
I am trying to get the last day of the month from a field numeric in SAS (ccyymm).
for example
201401 would be 31-01-2014.
I have managed to get the field to show as a date type field (still showing as ccyymm though) with the following code in a PROC SQL SELECT statement
year_month_field INFORMAT YYMMN6. AS year_month_date
I think that I found some code from another question that had been asked that should give me the last day of the month once I can get the full date.
INTNX ( MONTH , year_month_date , 1) -1
Will this work? If not any other suggestions would be appreciated.
Dan
Note the difference between how one would convert a yyyymm to "last day of the month"MMMYYYY format below, depending on whether yyyymm is character or numeric variable.
data test;
yyyymm_character='201401';
yyyymm_numeric=201401;
date1=intnx('month', input(yyyymm_character, yymmn6.), 1)-1;
date2=intnx('month', input(put(yyyymm_numeric,6.), yymmn6.), 1)-1;
format date1 date2 date9.;
/*date1=date2=31jan2014*/
run;
Alternatively, you can use the in-built options for intnx functions to automatically set any input date to the last day of the respective month. Use 'e' as shown below.
data test;
yyyymm_character='201401';
yyyymm_numeric=201401;
date1=intnx('month', input(yyyymm_character, yymmn6.), 0, 'e');
date2=intnx('month', input(put(yyyymm_numeric,6.), yymmn6.), 0,'e');
format date1 date2 date9.;
/*date1=date2=31jan2014*/
run