I am trying to convert a character to a date in sas and its driving me nuts that it is not working. The date is currently in character format as below
200609
I want to convert it to a similar date structure in proc sql but just cant get it to work. I am using the following code:
input(date,anydtdtm.) as Perf_Date format = date9.,
Can anyone suggest where I am going wrong?
Use the actual informat. You're using anydtDTM - DTM means date time. You could also try ANYDTDTE which is looking for a date.
YYMMN6. works though and you can be sure it's correct.
data want;
x='200609';
y=input(catt(x, '01'), yymmn6.);
format y date9.;
run;
proc print;run;
01SEP2006
Related
I need help in figuring out the date conversion logic in Snowflake. The documentation isn't clear enough on this.
In SQL Server, I would try
SELECT CONVERT(DATE, '20200730', 101)
and it gives me '07/30/2020'.
If I try the following in Snowflake,
to_varchar('20200730'::date, 'mm/dd/yyyy')
it gives me '08/22/1970'. Why would it give an entire different date? Need help in getting the logic with the correct date.
The issue with what you are doing is that you are assuming that Snowflake is converting your string of '20200730'::DATE to 2020-07-03. It's not. You need to specify your input format of a date. So, 2 options based on your question being a bit vague:
If you have a string in a table and you wish to transform that into a date and then present it back as a formatted string:
SELECT TO_VARCHAR(TO_DATE('20200730','YYYYMMDD'),'MM/DD/YYYY');
--07/30/2020
If the field in the table is already a date, then you just need to apply the TO_VARCHAR() piece directly against that field.
Unlike SQL Server, Snowflake stores date fields in the same format regardless of what you provide it. You need to use the TO_VARCHAR in order to format that date in a different way...or ALTER SESSION SET DATE_OUTPUT_FORMAT will also work.
Try select to_varchar(TO_DATE( '20200730', 'YYYYMMDD' ), 'MM/DD/YYYY'); which produces 2020-07-30
You may need to refer to https://docs.snowflake.com/en/user-guide/date-time-input-output.html#timestamp-formats
I have an xlsx dataset that I import using proc import. There is a column in excel that holds date values like:
2018-11-30 00:00:00
When I import it into SAS it automatically converts it into a number 43434.
When I try to cast this to a date: put(col,date9.), i get:
2078-12-01
What is happening? How can I get back the correct date. I tried mixed=yes option but it does not work with dbms=xlsx. When i do dbms=excel, it does not work as expected
Sometimes SAS imports the date as a raw Excel date. I don't know exactly why or when it does this, but I think it has something to do with the format of the date. If this happens, subtract the date by 21916 to convert from an Excel date to a SAS date.
data want;
set imported_from_excel;
date = excel_date - 21916;
run;
I have a Varchar like so:
23FEB2025
I am trying to convert it into a format like:
1994-02-23 or YYYY-MM-DD
I have tried select cast ('23FEB2025' as date format 'yyyy-mm-dd'); and sel convert(date,'23FEB2025')
There are other dates in the column that are formatted like 12DEC65.
I am now starting to assume that there is no simple way to convert this so I am asking for a little guidance. Would i need to take sub strings of the date and use a bunch of select case statements?
I was hoping to find a short way to do this but it seems there might not be one. I read on here that storing dates as a string is a bad idea and I fully subscribe to that notion now.
Thank you for any help or advice!
The format portion of casting a date is the input format. The output format is based on your locale and date settings. In your case, you want this:
select
cast ('23FEB2025' as date format 'ddMMMYYYY')
Which will return 2025-02-23.
I am rewriting code from oracle sql to sas proc sql and I have problem with this:
where demandes_fin.per_idt = to_char(add_months(to_date(fin.per_idt,'yyyymm'),1),'yyyymm'))
demandes_fin.per_idt and fin.per_idt are INT variables - for example 201612.
functions to_char, add_months, to_date are not working in SAS proc sql. I have tried to replace them using put, input, intnx, format but it didn't work as I expected.
I have tried to put this code in select statement to see values, that was generated:
intnx('month', input(put(fin.per_idt,6.),yymmn6.), 1) as dt1
fin.per_idt was 201701 and generated value was 20851.
Do you have any ideas how to code it?
Thank you so much.
Looks like you have already solved your problem. SAS stores dates as the number of days since 01JAN1960. The number 20,851 represents 01FEB2017 which is one month after January 2017.
If you want to keep your result as a SAS date value just add the FORMAT keyword to your definition so that the date will display in a format that humans can understand.
intnx('month', input(put(fin.per_idt,6.),yymmn6.), 1) as dt1 format date9.
If you want to convert it back into the strange integer value you started with then just add some more PUT() and INPUT() function calls.
input(put(intnx('month', input(put(fin.per_idt,6.),yymmn6.), 1),yymmn6.),6.)
I'm having a massive problem with a project that I just can't seem to get right. I'm trying to modify my data variable as its currently in a datetime format. The first data is 30Mar12:00:00:00. I've been advised to use the code below but it returns a value like 30mar60:5:30:00. I just want the date component and have no idea how the data even got exported in this format as it wasn't like this in Microsoft Access. I've tried several mods to this code but again just comes up completely blank or sends an error message.
proc sql; update dataset set date = DATEPART(date); quit;
Any advice would be greatly appreciated.
I'm guessing the variable date is numeric and formatted as DATETIME. Once you've carried out the conversion, just change the format to something like DATE9. A SAS datetime value is the number of seconds since 01 Jan 1960, whereas dates are the number of days since 01 Jan 1960. Both are stored as numbers, so using the correct format to display the value is key. Example below.
data _null_;
a='30Mar12:00:00:00'dt;
b=datepart(a);
c=b;
format a b datetime. c date9.;
put a b c;
run;