BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
thesasuser
Pyrite | Level 9

I was trying to convert this T SQL query to SAS  format

 

CREATE DATABASE ORG; SHOW DATABASES; USE ORG;

CREATE TABLE Worker ( WORKER_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, FIRST_NAME CHAR(25), LAST_NAME CHAR(25), SALARY INT(15), JOINING_DATE DATETIME, DEPARTMENT CHAR(25) );

INSERT INTO Worker (WORKER_ID, FIRST_NAME, LAST_NAME, SALARY, JOINING_DATE, DEPARTMENT)

VALUES (001, 'Monika', 'Arora', 100000, '14-02-20 09.00.00', 'HR'),

              (002, 'Niharika', 'Verma', 80000, '14-06-11 09.00.00', 'Admin'),

              (003, 'Vishal', 'Singhal', 300000, '14-02-20 09.00.00', 'HR'),

              (004, 'Amitabh', 'Singh', 500000, '14-02-20 09.00.00', 'Admin'),

              (005, 'Vivek', 'Bhati', 500000, '14-06-11 09.00.00', 'Admin'), 

             (006, 'Vipul', 'Diwan', 200000, '14-06-11 09.00.00', 'Account'),

             (007, 'Satish', 'Kumar', 75000, '14-01-20 09.00.00', 'Account'),

             (008, 'Geetika', 'Chauhan', 90000, '14-04-11 09.00.00', 'Admin');

 

This I changed to 

proc sql;
CREATE TABLE Worker ( WORKER_ID num, FIRST_NAME CHAR(25), LAST_NAME CHAR(25), 
SALARY num, JOINING_DATE char(19) , DEPARTMENT CHAR(25) );
INSERT INTO Worker 

VALUES (001, 'Monika', 'Arora', 100000, '14-02-20 09.00.00', 'HR') 
VALUES (002, 'Niharika', 'Verma', 80000, '14-06-11 09.00.00', 'Admin') 
VALUES (003, 'Vishal', 'Singhal', 300000, '14-02-20 09.00.00', 'HR') 
VALUES (004, 'Amitabh', 'Singh', 500000, '14-02-20 09.00.00', 'Admin') 
VALUES (005, 'Vivek', 'Bhati', 500000, '14-06-11 09.00.00', 'Admin') 
VALUES (006, 'Vipul', 'Diwan', 200000, '14-06-11 09.00.00', 'Account') 
VALUES (007, 'Satish', 'Kumar', 75000, '14-01-20 09.00.00', 'Account') 
VALUES (008, 'Geetika', 'Chauhan', 90000, '14-04-11 09.00.00', 'Admin');
quit;

I used the following to convert the joining_dates to SAS dates as follows :

data worker2;
set worker(Rename=(JOINING_DATE=JD));
format JOINING_DATE datetime23.;
JOINING_DATE=input(jd,  anydtdte23.);
run;

But the date come as like this only

01JAN1960:05:29:34

 

Is there any way to convert them to proper SAS dates?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Is your question really about how to specify a datetime literal?

Note sure what a value like '14-02-20 09.00.00' is supposed to mean, but for a guess you might have meant to use.

'14FEB2020:09:00:00'dt

The reason your values look like they are from 1960 is that you applied a datetime format to a date value.  Date values are stored as the number of days and datetime values are stored as the number of seconds.  If you want to convert those strings to datetime values then use the ANYDTDTM. informat instead of the ANYDTDTE. informat. The former will create a datetime value while the later creates a date value.  But since the variable name implies that you just want the date and don't care about the time of day part then perhaps you should leave the informat the same and chave the format used to display it to a date format like YYMMDD10. or DATE9. to the value instead and ignore the 9AM part of the original value.

 

In the long run it will probably be easier to write a data step to read the values instead of using SQL INSERT statements.

data worker;
  infile cards dsd dlm=',' truncover ;
  input WORKER_ID FIRST_NAME :$25. LAST_NAME :$25. SALARY JOINING_DATE :anydtdtm. DEPARTMENT :$25.;
  format  worker_id z3. joining_date datetime20.;
cards;
001,'Monika','Arora',100000,'14-02-20 09.00.00','HR'
002,'Niharika','Verma',80000,'14-06-11 09.00.00','Admin'
003,'Vishal','Singhal',300000,'14-02-20 09.00.00','HR'
004,'Amitabh','Singh',500000,'14-02-20 09.00.00','Admin'
005,'Vivek','Bhati',500000,'14-06-11 09.00.00','Admin'
006,'Vipul','Diwan',200000,'14-06-11 09.00.00','Account'
007,'Satish','Kumar',75000,'14-01-20 09.00.00','Account'
008,'Geetika','Chauhan',90000,'14-04-11 09.00.00','Admin'
;

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Is your question really about how to specify a datetime literal?

Note sure what a value like '14-02-20 09.00.00' is supposed to mean, but for a guess you might have meant to use.

'14FEB2020:09:00:00'dt

The reason your values look like they are from 1960 is that you applied a datetime format to a date value.  Date values are stored as the number of days and datetime values are stored as the number of seconds.  If you want to convert those strings to datetime values then use the ANYDTDTM. informat instead of the ANYDTDTE. informat. The former will create a datetime value while the later creates a date value.  But since the variable name implies that you just want the date and don't care about the time of day part then perhaps you should leave the informat the same and chave the format used to display it to a date format like YYMMDD10. or DATE9. to the value instead and ignore the 9AM part of the original value.

 

In the long run it will probably be easier to write a data step to read the values instead of using SQL INSERT statements.

data worker;
  infile cards dsd dlm=',' truncover ;
  input WORKER_ID FIRST_NAME :$25. LAST_NAME :$25. SALARY JOINING_DATE :anydtdtm. DEPARTMENT :$25.;
  format  worker_id z3. joining_date datetime20.;
cards;
001,'Monika','Arora',100000,'14-02-20 09.00.00','HR'
002,'Niharika','Verma',80000,'14-06-11 09.00.00','Admin'
003,'Vishal','Singhal',300000,'14-02-20 09.00.00','HR'
004,'Amitabh','Singh',500000,'14-02-20 09.00.00','Admin'
005,'Vivek','Bhati',500000,'14-06-11 09.00.00','Admin'
006,'Vipul','Diwan',200000,'14-06-11 09.00.00','Account'
007,'Satish','Kumar',75000,'14-01-20 09.00.00','Account'
008,'Geetika','Chauhan',90000,'14-04-11 09.00.00','Admin'
;

 

thesasuser
Pyrite | Level 9

Thanks Tom for your solution.

It worked like a charm.

My mistake was in using ANYDTDTE.  instead of  ANYDTDTM.

Thanks once again.

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 761 views
  • 0 likes
  • 2 in conversation