Hi there,
I have a table with dates stocked in a DATE9 format.
I want to set a macro variable to extract data from a specific date.
To convert my date, I use the following code :
%let date1 = 2018-12-31;
%let datefin = %sysfunc(inputn(&date1, yymmdd10.), date9.);
When I write %put &datefin.; it works and shows 31DEC2018.
However, when I try to extract datas with a PROC SQL, I got an error.
Here's the code I'm trying to run.
%let date1 = 2018-12-31;
%let datefin = %sysfunc(inputn(&date1, yymmdd10.), date9.);
PROC SQL;
CREATE TABLE TEST AS
(SELECT *
FROM TABLESOURCE
WHERE DATE_TABLE <= &DATEFIN.);
QUIT;
RUN;
Here's the part of the log showing an error:
35 31DEC2018
___________
ERROR 22-322 : Syntax error, expecting of the following: !, !!, &,........
ERROR 76-322: Syntax error, statement will be ignored.
Any ideas where I am doing something wrong? Any help would be greatly appreciated.
Thanks,
So it sounds like your original code is working. You do NOT have any date values that are from before 2019.
43 data test; 44 input date; 45 put date= date9. +1 date= comma7.; 46 cards; date=07JAN2019 date=21,556 date=30SEP2019 date=21,822
Wouldn't you need
"&DATEFIN."d
PROC SQL;
CREATE TABLE TEST AS
(SELECT *
FROM TABLESOURCE
WHERE DATE_TABLE <= "&DATEFIN."d);
QUIT;
RUN;
Don't format the macro variable. Just leave it as the numeric value of the date: %sysfunc(inputn(&date1, yymmdd10.));
If you use the formatted value you would have to use "&datefin."d for the comparison.
Or instead of bothering to convert yymmdd format text use
%let date1 = "31DEC2018"d;
and use: date_table le &date1.
I tried both of your solutions (@Reeza and @ballardw) and it doesn't work either. I don't understand why.
Is there a way that SAS is displaying the date in date9 format but in fact it could be coded in international format (YYYY-MM-DD) ? Is there a way to check this?
The code in my first post is exactly what I have.
I tried using
%let datefin = "31DEC2018"d;
and in the code I used :
PROC SQL;
CREATE TABLE TEST1 AS
( SELECT *
FROM TABLESOURCE
WHERE DATETABLE le &datefin.
);
QUIT;
RUN;
Still do not works. It's annoying because I try simple things and maybe it's just me :S
2018-21-31
is not a valid date as well.
I fix the typo, thanks.
Does
PROC SQL;
CREATE TABLE TEST1 AS
SELECT *
FROM TABLESOURCE
WHERE DATETABLE le "31DEC2018"d;
QUIT;
work?
Examples of both approaches - both work.
%let date1 = 2005-12-01;
%let datefin = %sysfunc(inputn(&date1., yymmdd10.));
proc sql;
create table test1 as
select *
from sashelp.stocks
where date = &datefin;
quit;
%let date1 = 2005-12-01;
%let datefin = %sysfunc(inputn(&date1, yymmdd10.), date9.);
%put &datefin;
proc sql;
create table test2 as
select *
from sashelp.stocks
where date = "&datefin."d;
quit;
@jpprovost wrote:
The code in my first post is exactly what I have.
I tried using
%let datefin = "31DEC2018"d;
and in the code I used :
PROC SQL; CREATE TABLE TEST1 AS ( SELECT * FROM TABLESOURCE WHERE DATETABLE le &datefin. ); QUIT; RUN;
Still do not works. It's annoying because I try simple things and maybe it's just me :S
@jpprovost wrote:
The code in my first post is exactly what I have.
I tried using
%let datefin = "31DEC2018"d;
and in the code I used :
PROC SQL; CREATE TABLE TEST1 AS ( SELECT * FROM TABLESOURCE WHERE DATETABLE le &datefin. ); QUIT; RUN;
Still do not works. It's annoying because I try simple things and maybe it's just me :S
What does that mean? Show the log. Did you get an error or wrong number of observations?
Is DATETABLE really a DATE value? Perhaps it is a DATETIME value? If it is a DATETIME value then unless it is before 6AM on January 1, 1960 then its value is probably greater than any DATE value you might compare it to.
where DATETABLE le "31DEC2018:00:00"dt;
or
where datepart(DATETABLE) le &datefin.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.