What is the syntax to insert a date macro variable into a table?
proc sql; select max(rundate) into: testvar SEPARATED BY '' from testtable; create table rundatelist ( tablename varchar(100), rundate date); insert into rundatelist (tablename, rundate) values ('table1', to_date(&testvar)); /*this line does not work*/ select * from rundatelist; quit;
Is your rundate values are in datetime format, then you might need to change your create table
data testtable;
format rundate datetime26.;
rundate='17OCT2018:14:45:32'DT;output;
rundate='18OCT2018:14:45:32'DT;output;
run;
proc sql;
select max(rundate) into: testvar SEPARATED BY '' from testtable;
create table rundatelist (
tablename varchar(100),
rundate num format=datetime26.);
insert into rundatelist (tablename, rundate) values ('table1', &testvar); /*this line does not work*/
select * from rundatelist;
quit;
If you want to insert only date instead of datetime then use datepart() to extract date.
proc sql;
select datepart(max(rundate)) into: testvar SEPARATED BY '' from testtable;
create table rundatelist (
tablename varchar(100),
rundate date);
insert into rundatelist (tablename, rundate) values ('table1', &testvar); /*this line does not work*/
select * from rundatelist;
quit;
Reeza if I remove to_date(), the log shows.
24OCT2018:04:34:39
_______
22
76
ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant,
a missing value, ), +, ',', -, MISSING, NULL, USER.
ERROR 76-322: Syntax error, statement will be ignored.
Do you know what the correct syntax would be if you were hard-coding a value instead of referring to a macro variable?
To_date() is an oracle function yes? Maybe:
to_date("&testvar.")
You may need to specify a format also.
Or do you mean this function:
Because that is expecting a number of days, so you would need to input() the text from that macro variable.
Is your rundate values are in datetime format, then you might need to change your create table
data testtable;
format rundate datetime26.;
rundate='17OCT2018:14:45:32'DT;output;
rundate='18OCT2018:14:45:32'DT;output;
run;
proc sql;
select max(rundate) into: testvar SEPARATED BY '' from testtable;
create table rundatelist (
tablename varchar(100),
rundate num format=datetime26.);
insert into rundatelist (tablename, rundate) values ('table1', &testvar); /*this line does not work*/
select * from rundatelist;
quit;
If you want to insert only date instead of datetime then use datepart() to extract date.
proc sql;
select datepart(max(rundate)) into: testvar SEPARATED BY '' from testtable;
create table rundatelist (
tablename varchar(100),
rundate date);
insert into rundatelist (tablename, rundate) values ('table1', &testvar); /*this line does not work*/
select * from rundatelist;
quit;
Suryakrian,
Thanks for your answer. I included dt after to use the logic with a macro.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: