SAS Datetime and IF statements - datetime

Hello I am trying to set a date 01/02/2015 to 01/09/2015. I am using a IF statement but I think since the column has as format of mmddyy10 and informat datetime20 its not working.
The column looks like this
01/09/2015
01/02/2015
05/23-2015
So I want it to look like this
01/09/2015
01/09/2015
05/23-2015
Is there a function in SAS that allows me to do this? Or will I have to convert the column to a Char and do my If statement that way.

As long as it's a date value - so the unformatted value is something around 20000 - you can use date constant.
data want;
set have;
if datevar = '02JAN2015'd then datevar='09JAN2015'd;
run;
This is irrespective of the format applied to the column. Format is not variable type; it's something like "When I look at this variable, please print it nicely using this format." The underlying value is the same for all dates: some number of days since 1/1/1960.
Date constants are always represented using DATE9. format (DDMONYYYY).
If it's actually a datetime, then the format would not be MMDDYY10. but something else. Then you use a datetime constant:
if datetimevar = '02JAN2015:00:00:00'dt then ...
or convert it to date using datepart first.
if datepart(datetimevar) = '02JAN2015'd then ...

Related

How to create a DateTime column From Date and Hour column in Power Query

I have this column in a table:
2019-10-01 01
2019-10-01 02
2019-10-01 03...
How can I use Power Query to convert it (or create a new column) into a proper DateTime column, where "01,02,03" values above are the hours? I'm dealing in dates and hours only.
Power Query will not recognize it natively.
If you append ":00" to the end of the column, it should be able to interpret it as a DateTime format.
Table.TransformColumns(Source,{{"Col", each DateTime.FromText(_&":00"), type datetime}})
I used the following input table
This worked for me
let
source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
splitByDelimiter = Table.SplitColumn(source, "Input", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, true), {"Date", "Time"}),
changedType = Table.TransformColumnTypes(splitByDelimiter,{{"Date", type date}, {"Time", type time}})
in
changedType
with the following result
If you have your column named Column1, like this:
You can use this code:
Table.TransformColumns(Source,{"Column1", each #datetime(Number.From(Text.Range(_,0,4)),Number.From(Text.Range(_,5,2)),Number.From(Text.Range(_,8,2)),Number.From(Text.Range(_,11,2)),0,0)})
To get this:
Just copy the code above, click on the , and paste the code in the formula bar. If your column is not named Column1, replace the reference to Column1 with the name of your column.

Creating datetime data on hour frequency by using date and hour column in integer type in Hive

I have a table including date column and hour column which is an integer type column varying from 0 to 24. I need to combine these two fields and create an hourly composite datetime field.
However, I was able to create that kind of variable by using || and cast. But I am unable to transform this code to Hive editor syntax. Can you help me with this problem
SQL Code:
CAST(CAST(CAST(DATE_OF_TRANSACTION AS FORMAT 'yyyy-mm-dd') AS VARCHAR(11))||' '||CAST(CAST( BasketHour AS FORMAT '99:') AS VARCHAR(10))||'00:00' AS TIMESTAMP(0)) Date_Time
Thank you very much
For example like this:
cast(concat(DATE_OF_TRANSACTION, ' ', lpad(BasketHour ,2,0),':00:00.0' ) as timestamp)

Wrong formatting datetime sas

I want the system output to be: 14FEB18:11:53:49. I'm applying the format: datetime. But the output is 01JAN60:05:53:49.
data test;
date = today();
format date datetime.;
run;
try below. Your applying datetime format to date and hence your getting wrong result. In sas if you see anything with 1960 as year most of the time, it because you are reading data wrongly or make a wrong calculation on date. Today() gives current date whereas datetime() gives current datetime.
data test;
date = datetime();
format date datetime.;
run;

how to convert a string date variable into datetime variable in sas

I have a string variable for datetime. Sometimes it is a whole number like 3040000 sometimes a decimal value like this 3130215.123.
I would like to convert this into a date time variable like mm-dd-yyyy.
Thanks in advance.
add: I think the value 3130215.123 refers to feb-15-2013 12:30:00.
I think these are
3YYMMDD.HHMM
3130215.123 -> 3|13|02|15|12|30
(the last 0 is assumed).
So, you need:
length year month day hour minute second $2;
year=substr(dtvar,2,2);
month=substr(dtvar,4,2);
day=substr(dtvar,6,2);
hour=translate(subpad(dtvar,9,2),'0',' ');
minute=translate(subpad(dtvar,11,2),'0',' ');
second=translate(subpad(dtvar,13,2),'0',' ');
then
new_dtvar=dhms(mdy(month,day,year),hour,minute,second);
Dates, Times, and Datetimes are stored as doubles. Only the format matters for display.
try
var1 = input(var,best.);
format var1 datetime19.;
in a data step to apply the format. Then look at the results.
Based on comments on the original question, the statements would be:
format var_date ddmmyyd10. var_time time5. var_datetime datetime19.;
var_date=input(put(int(17000000+(var)),10.),yymmdd10.);
var_time=input(put((var-int(var))*1e6,z6.),hhmmss6.);
var_datetime=dhms(var_date,hour(var_time),minute(var_time),0);
Haven't had a chance to test, so feel free to comment with any errors you get.

Date format in SAS

I have a SAS code that I need to convert into R.
My SAS code is something like this -
proc sql;
create table data as
select a.*,b.qty from Sales as a inner join Units as b
on a.id=b.id and put(a.date,yymmn6.)=put(c.date,yymmn6.)
quit;
I know that put(a.date,yymmn6.) converts the date into a SAS date value. But what does a.date become after this function? If date=01jan2012, put(a.date,yymmn6.) makes it as some SAS value that represents 201201 or 20120101? i.e. the SAS value created will stand for the whole date or just the year and mon of the date?
Currently, I am writing the R code for this as -
data <- sqldf("select a.*,b.qty from Sales as a inner join Units as b
on a.id=b.id and a.date=c.date")
Should I be doing it as -
Sales$date <- as.yearmon(Sales$date)
Units$date <- as.yearmon(Units$date)
data <- sqldf("select a.*,b.qty from Sales as a inner join Units as b
on a.id=b.id and a.date=c.date")
I don't have access to SAS and hence, I cannot try this out on a sample data. Any help would be great. Thanks!
put(a.date,yymmn6.) converts a numeric date value to a character value stored as yyyymm (e.g. 201201). Therefore the join condition is matching all dates where the month and year are the same, but not necessarily the day.
I'm not sure of the best way of achieving this in R, but you seem to have some ideas on this.
Hope this helps.
When you use put(a.date,yymmn6.) the output of that function is a character. Put takes a numeric input and format and outputs the formatted numeric value as character. input function does the opposite.
data mydata;
sas_numeric_date = "01jan2012"d;
sas_yyyymm_char_date = put(sas_numeric_date, yymmn6.);
sas_yyyymm_numeric_date = input(sas_yyyymm_char_date, yymmn6.);
output;
sas_numeric_date = "29Feb2012"d;
sas_yyyymm_char_date = put(sas_numeric_date, yymmn6.);
sas_yyyymm_numeric_date = input(sas_yyyymm_char_date, yymmn6.);
output;
format sas_numeric_date sas_yyyymm_numeric_date date9.;
run;
sas_numeric_date sas_yyyymm_char_date sas_yyyymm_numeric_date
01Jan2012 201201 01Jan2012
29Feb2012 201202 01Feb2012
So, when you apply the yymmn6. as informat on sas_yyyymm_char_date - which itself is in yyyymm format, the resulting value is numeric and day part in the date defaults to the first day of the month as shown above.

Resources