convert datetime to date in proc-sql sas [duplicate] - sas

This question already has answers here:
SAS 9.3 DATETIME VARIABLE FORMAT AS DATE
(8 answers)
Closed 8 years ago.
I'm trying to convert datetime22.3 variable to ddmmmyy10. in proc sql, this is giving me ****** in the output column.
How can I get the correct values in the output column?

You need to convert original SAS DATETIME value (think of it as data type) to SAS DATE value using DATEPART() function and apply appropriate format:
proc sql;
create table work.abc
as select DISTINCT a.Account_Id,
DATEPART(a.Billing_Dt) format ddmmyy10. as Bill_date
from abc table;
quit;

Related

Convert time (date type) to numeric in proc SQL (SAS)

I have a datetime variable (x= '19NOV2021: 10:00:00') which is a date type. I am trying to just get the hour and convert it to numeric. In this case, just 10.
proc sql;
create table abc as
select timepart(x) as x format=time2.
The above outputs 10 as a date type. I use the below to try to convert it to numeric but get 3 (numeric type) as my output:
proc sql;
create table def as
select input(put(x,best.),8.) as x
How can I convert the time to a numeric and get 10 as my output?
Use hour().
timepart() returns a SAS time as the number of seconds that have elapsed since midnight. You can use hour(timepart(x)), but it's not necessary since hour works on both datetimes and times.

Add a number of days to SAS datetime format

I need help with SAS datetime format.
Dataset(including desired column exp_dt):
datetime valid exp_dt
4OCT2017:13.00.00 1 5OCT2017:13.00.00
4OCT2017:15.20.00 7 11OCT2017:15.20.00
6OCT2017:08.00.00 30 5NOV2017:08.00.00
So, I need to add valid values (number of days) to datetime.
I've just started with SAS Base and I am not sure if any other datetime format is acceptable.
I've tried with this, but not sure if even going in right direction:
PlannedSchedTime = datetime ;
Postunit = 'DAY' ;
postval = valid ;
exp_dt = put(intnx(Postunit,PlannedSchedTime,postval,'same'),datetime20.);
put exp_dt= ;
run;
Also, I'm working on project in SAS Enterprise Guide, so maybe there is easier way through the GUI tasks?
Here a code sample that will print your desired result. Like the other answer states, DTDAY will tell SAS to add days when the base value is a datetime instead of a date.
data datetimes;
informat datetime anydtdtm. valid best12.;
format datetime datetime20.;
input datetime valid;
cards;
4OCT2017:13.00.00 1
4OCT2017:15.20.00 7
6OCT2017:08.00.00 30
;
run;
data datetimes_added;
set datetimes;
format exp_dt datetime20.;
exp_dt = intnx('DTDAY',datetime,valid,'SAME');
put exp_dt = ;
run;
You are definitely on the right path! Because you are dealing with datetime values, replace DAY by dtDAY.
If you don't want exp_dt to be a character column, don't use the put function but rather a format (e.g. format exp_dt2 datetime20.;).

Using Macrovariable in a Date Variable (SAS) [duplicate]

This question already has answers here:
Why won't my macro variable resolve?
(2 answers)
Closed 6 years ago.
I am programming in SAS and I am trying to figure out how to use a macrovariable within a date variable;
an example would be something like
%let year = 2015;
data test;
set;
date = '01JAN&year.'d;
run;
Thanks for your help!
Use double quotes and format the variable using the date9. format:
%let year = 2015;
data test;
format date date9.;
date = "01JAN&year."d;
run;

How to convert Datetime into SAS format

I am working with a huge number of observations in different tables in different versions.
I will use a date %let date_to_view = "20JAN2014:16:10"dt;
But how to convert this date into SAS format?
I knew how to Convert SAS data type (use proc sql):
dhms("01JAN1970'd,3,0,i.valid_dttm/1000) format datetime20.
I see date 20JAN2014:16:34:10 is 1390224849927 but how to convert it into code?
In your formula dhms("01JAN1970'd,3,0,i.valid_dttm/1000) you are converting a number that represents the number of milliseconds since 01JAN1970 to a SAS datetime value that represents the number of seconds since 01JAN1960. You also appear to be adding 3 hours.
So it sounds like your question is how to convert a SAS DATETIME value into a Unix timestamp value. So just reverse the arithmetic.
Your formula to convert from a Unix timestamp to a SAS datetime was:
sasdt2 = '01JAN1970:00:00'dt + '03:00't + unix_timestamp2/1000 ;
So to convert from a SAS datetime value to a Unix timestamp use:
unix_timestamp1 = 1000*(sasdt1 - '01JAN1970:00:00'dt - '03:00't) ;
"20JAN2014:16:10"dt is already in the correct SAS date (datetime) format, but as a date literal. SAS stores this as a number, representing the number of seconds since 01JAN1960:00:00:00.
If you just want the date component of the datetime, use the datepart() function, and format the result accordingly, e.g. date9..
data want ;
dt = "20JAN2014:16:10"dt ;
date = datepart(dt) ;
format dt datetime19. date date9. ;
/* To have 'date' show as the unformatted value, simply remove the format */
format date best32. ;
run ;

Boxplots where date grouped by year

I recently asked a question about grouping in SAS. Drawing on that question, and using the same data set, I am struggling to make a box plot.
The data look like this:
Date Close Volume
12/31/2014 222.41 2402097
12/30/2014 222.23 2903242
12/29/2014 225.71 2811828
12/26/2014 227.82 3327016
12/24/2014 222.26 1333518
12/23/2014 220.97 4513321
12/22/2014 222.6 4806917
12/19/2014 219.29 6910461
12/18/2014 218.26 7483349
12/17/2014 205.82 7367834
12/16/2014 197.81 8426105
12/15/2014 204.04 5218252
12/12/2014 207 7173782
This data set actually covers two full years 2013 - 14. I would like a boxplot for each year and variable (Close and Volume).
Here is what I tried:
proc boxplot data=tsla;
class Date;
format Date year.;
plot Close*Date;
run;
But that returns an error "
ERROR 180-322: Statement is not valid or it is used out of proper order.
162 format Date year.;
163 plot Close*Date;
164 run;
"
What's the right order then?
How can I get SAS to give me 4 boxplots total? 2 variables (Close and Volume) and over two years (2013 - 14)?
There is no class statement in proc boxplot.
First, add a "year" variable using a data step.
data tsla2;
set tsla;
year=year(date);
run;
Sort by year:
proc sort data=tsla2;
by year;
run;
Use by statement in proc boxplot:
proc boxplot data=tsla2;
by year;
plot close*year;
plot volume*year;
run;
If you want all the years plotted together for each variable, there is no need to sort or use a by statement. Just do:
proc boxplot data=tsla2;
plot close*year;
plot volume*year;
run;