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

Good morning,

 

I found a script on our server and I am not sure how to interpret it.

 

Here's a simplify version of the code.  So we are trying to create a table named mytable from the contains of table10 which contains few variables , including the variable from and to.  The variable from to contains date like 20081231 to 99991231.

 

So suppose that we have %let dateproduction='20200131'

 

I believe the le statement means less or equal but how will work the where statement?

 

Thanks for your help

 

 

 

proc sql;
create table mytable as
select *
from table10
where( calculated from le "&DATEPRODUCTION." le calculated to
);
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Are FROM and TO character strings like '20081231' or numbers like 20,081,231?

If they are strings then generated SAS code in the WHERE clause needs to contain with single or double quotes, but your example code is going generate both double and single quotes.  It they are numbers then you do not want any quotes.

 

The LE operator means less than or equal to, which you also write as <= .

 

You do not need the CALCULATED keyword in this example if FROM is a variable that is coming from the input TABLE10.  If FROM was the name of some generated variable listed in the SELECT statement then you would need the CALCULATED keyword.

 

If FROM and TO are character you could keep the quotes in the macro variable:

%let dateproduction='20200131';
proc sql;
create table mytable as
  select *
  from table10
  where  (from le &DATEPRODUCTION. le to)
;
quit;

Or not put the quotes in the macro variable and instead add them back into the generated code.

%let dateproduction=20200131;
proc sql;
create table mytable as
  select *
  from table10
  where  (from le "&DATEPRODUCTION." le to)
;
quit;

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

Do you mean you want to extract all records where &dateproduction is greater than or equal to variable FROM and less than or equal to variable TO?

--
Paige Miller
Tom
Super User Tom
Super User

Are FROM and TO character strings like '20081231' or numbers like 20,081,231?

If they are strings then generated SAS code in the WHERE clause needs to contain with single or double quotes, but your example code is going generate both double and single quotes.  It they are numbers then you do not want any quotes.

 

The LE operator means less than or equal to, which you also write as <= .

 

You do not need the CALCULATED keyword in this example if FROM is a variable that is coming from the input TABLE10.  If FROM was the name of some generated variable listed in the SELECT statement then you would need the CALCULATED keyword.

 

If FROM and TO are character you could keep the quotes in the macro variable:

%let dateproduction='20200131';
proc sql;
create table mytable as
  select *
  from table10
  where  (from le &DATEPRODUCTION. le to)
;
quit;

Or not put the quotes in the macro variable and instead add them back into the generated code.

%let dateproduction=20200131;
proc sql;
create table mytable as
  select *
  from table10
  where  (from le "&DATEPRODUCTION." le to)
;
quit;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 647 views
  • 0 likes
  • 3 in conversation