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?
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 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
I need to convert a string in YYYYMMDD format into a numeric DATE9 format in SAS.
I can convert the string to YYYYMMDD8, but am struggling to then convert that into DATE9 format. I thought it would be a case of just putting in a format statement for the variable and setting it as DATE9 since it is already in a numeric date format, but doing so just produces asterisks.
I've tried to reproduce some example code. The dataset date1 is provided. I've converted it to YYYYMMDD8. as per step 2. It's step 3 I cannot get to work. I've tried accepted solutions provided by answers to similar questions, but I can't get it to work. So I may be doing something wrong.
So in short, I need either a working version of Step 3, or a simplified Step 2 that converts the string to date9 in one step.
* STEP 1 - Provided;
data date1;
FORMAT file_date $8.;
file_date = '20191209';
run;
* Step 2 - Convert to YYYYMMDD8.;
PROC SQL;
CREATE TABLE date2 AS
SELECT INPUT(file_date,yyyymmdd8.) AS file_date2 FORMAT YYYYMMDD8.
FROM date1;
* Step 3 - Convert to DATE9. (Results in asterisks in place of the actual date);
DATA date3;
SET date2;
FORMAT file_date3 date9.;
file_date3 = file_date2;
RUN;
The issue here is the format yyyymmdd does not exist, it's just yymmdd. For simplicity working with SAS dates, you should use the informat anydtdte. So step 2 can be simplified to the following:
proc sql;
create table date2 as
select input(file_date,anydtdte8.) as file_date2 format=date9.
from date1;
quit;
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.);
I have the following Variable called Date in an excel file which I'm reading into SAS:
Date
May2005
June2005
July2005
..
July2015
Both the format and the informat are characters ($8)
I wanted to convert these into a SAS Date variable.
How can I accomplish this task?
I thought about using substr to first create a month and year variable,
then use proc format to convert all the months to numeric (e.g 'jan' = 1).
The use the mdy date function to create a new date. But I wonder if there is a shorter way to accomplish this task?
You can use the ANYDTDTE. informat if you prepend a day to your month+year string.
data want ;
set have ;
actual_date = input('01'||date,anydtdte.);
format actual_date date9.;
run;
Note that the FORMAT or INFORMAT attached to the character variable is meaningless, but having a variable of only length 8 will not allow room to store longer month names. Perhaps the length got set to only 8 because your particular example set of data did not include any longer month names.
If you are running such an old version of SAS that the ANYDTDTE. informat does not exist or does not work with fully spelled out months then you will need to work a little harder. You could transform the string into DATE9 format.
actual_date = input
('01'||substr(date,1,3)||substr(date,length(date)-3)
,DATE9.);
As #Tom hints towards, you have to use an informat that SAS can interpret as a numeric value when reading in character dates. I'm not sure if there is one that reads MONTHYYYYw., (naturally, ANYDTDTE works but I prefer to avoid it). In this case, I would use MONYYw., combined with substr to get the length 3 Month abbreviation and the 2 digit year:
data have;
input Date $13.;
datalines;
January2005
Feburary2005
March2005
April2005
May2005
June2005
July2005
August2005
September2005
October2005
November2005
December2005
;
run;
data want;
set have;
Date2 = input(SUBSTR(Date,1,3)||SUBSTR(Date,length(date)-1,2),MONYY13.);
Format Date2 DATE8.;
run;
proc print data = want; run;