Trouble converting 02FEB22:00:00:00 to yyyymmddhhmmss in SAS - sql

I am trying to convert a variable with this format 02FEB22:00:00:00
to yyyymmddhhmmss. I have tried it several different ways and can't seem to get it right. Can anyone help me?

Datetime variables just contain the number of seconds since 1960. There is no need to convert the variable if you want to display it with a different format.
You could create a custom format that will display the current numeric variable as that 14 digit string using a picture format:
proc format ;
picture ymdhms (default=14)
low-high = '%Y%0m%0d%0H%0M%0S' (datatype=datetime)
;
run;
And then just use that new format to display the current variable.
proc print data=have;
format dtvar ymdhms. ;
run;
If you wanted to convert it to a character variable that contains that 14 digit string you could just remove the T from the string created by the B8601DT15. format.
data want;
set have;
length charvar $14;
charvar=compress(put(dtvar,b8601dt15.),'T');
run;

Related

SAS: PROC SQL: How to convert a character format column to time format

I have a column which is character format and has entries as hh:mm:ss. How can I convert character format to time format using proc sql in sas?
You should be able to use the input function with a format (time8.) to convert the value. The original column will not change its type from char, so you can create another column to hold the numeric value.
If you need something else then please edit your question with an expanded explanation and an example.
/* set up data */
data have;
input char_time : $8.;
datalines;
00:00:00
01:02:03
23:59:59
;
/* create a column in time8. format */
proc sql noprint;
create table
want as
select
char_time
,input(char_time,time8.) as num_time format = time8.
from
have
;
quit;

Converting a character date to SAS Date in PROC SQL

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

Convert Month_Dt In sas

I have a sample data in this format as char.
Month
2008-09
2007-10
2008-09
How to get the above as date '01sep2008' (SAS DESIGN STUDIO)
There are a few ways to do this. Dirk has a good method concatenating a day onto the string and using the INPUT() function.
You could also use the yymmn6. informat with the INPUT(). That requires you to strip the '-' from the string.
data test;
x = '2008-05';
format y date9.;
y = input(compress(x,"-"),yymmn6.);
put y;
run;
You could use the MDY() to specify the Month, Day and Year. Here you use the SCAN() function to pick out the month and year from the string using '-' as a delimiter.
data test;
x = '2008-05';
format y date9.;
y = mdy(scan(x,2,'-'),1,scan(x,1,'-'));
put y;
run;
Use an input function
An input function is a function that interpretes a character variable using an informat. _(This is not exactly the same as an input statement, which interpretes text in an infile according to an informat.)
There might be an informat that imidiately interpretes strings like 2008-09 correctly, but I don't know one, so I just add a _01 to it and apply the informat yymmdd10.
Example
Depending on your situation (the data is inform a file or a dataset / you prefer data steps or sql / ...) the full solution would look like this:
data Before;
input the_date $7.;
datalines;
2008-09
2007-10
2008-09
;
data After;
set Before (rename=(the_date=old_date));
format the_date date9.;
the_date = input(old_date||'-01', yymmdd10.);
* while testing the code, uncomment the following line :
drop old_date;
proc print;
run;

How to convert sas binary datetime to dd/mm/yyyy in webout file

I've a table named cust_field_value_l in sas server under Sasoprsk library having a column name value_dt who store date data.
When I get data from this table it comes in binary data as 1.827E9 or 1.826E9 and gives ***** when i print it in my webout file.
proc sql noprint;
create table date(date datetime);
quit;
proc sql noprint;
insert into date(date)
select value_dt
from Sasoprsk.cust_field_value_l;
quit;
Now my date table has data as 1.826E9 etc.
data _null_;
put "<html><body>";
run;
data _null_;
put numberOfObservations=;
set work.date nobs=numberOfObservations;
file _webout;
put date;
run;
data _null_;
put "</body></html>";
run;
I'm receiving ***** in my webout file
I've tried this too but it only giving me wrong date
proc sql noprint;
insert into date(date)
select (today()-datepart((value_dt / 1000.0)+315601200))
from Sasoprsk.cust_field_value_l;
quit;
this code giving me only 03NOV07 for all the date values.
I don't know how can i get correct date value from db and show them in webout file;
value_dt has all the values in 23NOV2017:00:00:00 formate.
value_dt
23NOV2017:00:00:00
15NOV2017:00:00:00
20NOV2017:00:00:00
without comparing with today()
date
1.827E9
1.826E9
1.827E9
and its giving me ***** for every row
with comparing with today()
date
03NOV07
03NOV07
03NOV07
and printing 03NOV07 for every date data
SAS only has two data types. Fixed length character strings and floating point numbers. DATE values are stored as the number of days since '01JAN1960'd and DATETIME values are stored as the number of seconds since '01JAN1960:00:00'dt. The numbers you are displaying are consistent with a datetime value. The TODAY() function (also know as the DATE() function) will return a DATE value. If you want a DATETIME value use the DATETIME() function instead.
If you want users to supply date values (like '03NOV2007'd) to filter your datetime variable then you will need to convert one or the other before comparing them. Say you have a datetime variable name TIMESTAMP and you want to find events that occured today you could use something like
where today() = datepart(timestamp)
If you are creating a new data set by reading values from an existing dataset then just use a DATA step. No need to pre-creating the dataset and then "inserting" the observations into it. Or to bother with using PROC SQL. Just create the dataset you want.
data date ;
set Sasoprsk.cust_field_value_l ;
keep value_dt ;
rename value_dt=date ;
format value_dt datetime20. ;
run;
But if the whole purpose is just to print the values to the _WEBOUT fileref then there is no need to make a copy of the data first. Just print directly from the source data.
data _null_;
put numberOfObservations=;
set Sasoprsk.cust_field_value_l nobs=numberOfObservations;
file _webout;
put value_dt datetime20.;
run;
If you only want to print the date part of the datetime value then you can use the DTDATE9. format instead.

SAS SQL Datepart function returning odd values

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;