BookmarkSubscribeRSS Feed
J_C
Calcite | Level 5 J_C
Calcite | Level 5

Hi,

I have not been programming for long and below is what I would like to do, but clearly the syntax is wrong.

proc sql;

create table good as

select *

from (case when date<'10/31/2014' then table1

               else table2)

;

quit;

In other words, depending on the date, I either want to pick table 1 or table 2.

Another way of doing it could be name change, as in..... if date<'10/31/2014' then rename table1 to good else rename table2 to good.

Thanks in advance,

J-C

5 REPLIES 5
stat_sas
Ammonite | Level 13

Hi,

Something like this may be useful in selecting observations based on condition.

proc sql;

create table good as

select *

from table1 where date<'31OCT2014'd

union all

select *

from table2 where date>='31OCT2014'd;

quit;

art297
Opal | Level 21

If date is referring to today's date, you could use something like the following:

%macro doit;

  proc sql;

    create table good as

      select *

        from

          %if date()<'31OCT2014'd %then table1;

          %else table2;

    ;

  quit;

%mend;

%doit

J_C
Calcite | Level 5 J_C
Calcite | Level 5

Thanks for your input and came up with this not along after posting the question.

Can you let me know if this works as well (obviously less efficient then yours)?

Fyi, &date. is already defined.

data _test;

if &date.<September then table_name= 'table 1';

else table_name='table 2';

run;

proc sql no print;

select table_name into:view_select

from _test;

quit;

proc sql;

create table good as

select *

from &view_select.

;

quit;

It seems to work so far, but do you see a case where it might fail?

Thanks,

J-C

art297
Opal | Level 21

Not sure what you're comparing with:

if &date.<September


Reeza
Super User

In addition to what Art has noted:

1. Dates in SAS for comparison need to be in the form: "DDMONYYYY"d i.e. "01Jan2014"d, so your date of 10/31/2014 may need to be transformed.

2. You can create the macro variable directly in the first data step using call symput/x

Assuming &date is a SAS date then you could do something like the following

data _test;

if month(&date.)<9 then call symputx('view_table', 'table 1)';

else call symputx('view_table', 'table 2');

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 5 replies
  • 773 views
  • 0 likes
  • 4 in conversation