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;
Related
I have a query which uses date2
call symput('date2',strip(" ' "||put(intx('month',today(),-2,'B'),date9.))||" ' "));
%put &date2;
which basically gives the first day of 2 months prior to running date
and I Have another date which is '30jan2015'd
I want to write a where condition for date column such as
where datecolumn >= '30jan2015'd and datecolumn <= &date2;
When I tried this I am getting an error saying Expression using greater than or equal has components that are of different data types.
Could anyone help me on how I should change the query to make the where condition run, please?
A SAS Date value is either a number, representing number of days from 01-JAN-1960, or a date literal, which is a construct of form "<day>-<month>-<year>"D.
Macro variables are resolved using the & symbol in source code, not $. Are you coming to SAS from a different coding language such as PHP ?
Because your ranges check is endpoint inclusive you can simplify the where statement you are coding by using a BETWEEN operator instead.
Example:
* place an unformatted date value (i.e. data value number) in macro variable;
data _null_;
call symput('date2',intx('month',today(),-2,'B'));
run;
* more code using a date literal and resolved macro variable;
data myDateRangeSubset;
set BigData;
where datecolumn between '30jan2015'd and &date2;
run;
If you are still getting log messages about different data types you will likely have to convert the datecolumn from string to the appropriate SAS date value.
where input(datecolumn,date9.) between '30jan2015'd and &date2;
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 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
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 have a table with two columns, all of them are datetime value
Such as, Column A with value ‘07/09/2012 14:13:34’
Now, I want to update column A to yyyymmdd by statement
Update Change_Date
SET A = CONVERT(VARCHAR(8),A,112)
It shows succsessful message but with no effect (no update value to 20120907) in my table Change_Date.
Any help will be greated, thank you!
A datetime fields saves a date time. How you see that date time is a result of the tool you're using to inspect the data, whether it is Management Studio, or your own software that's printing something from the database.
I strongly recommend keeping it as a datetime field. This will allow you to do date-related operations, such as subtractions and comparisons. If you want to change how your users see the date, then format your date at the presentation layer.
What's happening in the code you've posted is that you're setting the value of A to the same date that it already is. The fact that you're setting that value by means of a string in another format has no relation, SQL server will always have to parse your string input into a date that it can understand. This is why you're not getting an error message. The operation is working, only it's not changing anything.
You can select the date column in specified format or make a view which selects the column value in yyyymmdd format:
SELECT CONVERT(VARCHAR(8), A, 112) FROM Change_Date
It's because the datatype of the column is DATE or DATETIME and it has specific format. If you want to update the column with specific format, make another column and make its datatype VARCHAR. I believe 112 is yyyymmdd format.
I strongly suggest that you keep it AS IS. Database is the storage of data and not for viewing purposes. It is easy to perform task for dates if your data type is DATETIME or DATE. If for instance you want to retrieve the dates with specific format, that's the time you convert your date.
Hope this makes sense.