Help using Base SAS procedures

Condtional renaming or choosing of a dataset.

Reply
Occasional Contributor J_C
Occasional Contributor
Posts: 18

Condtional renaming or choosing of a dataset.

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

Trusted Advisor
Posts: 1,228

Re: Condtional renaming or choosing of a dataset.

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;

PROC Star
Posts: 7,474

Re: Condtional renaming or choosing of a dataset.

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

Occasional Contributor J_C
Occasional Contributor
Posts: 18

Re: Condtional renaming or choosing of a dataset.

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

PROC Star
Posts: 7,474

Re: Condtional renaming or choosing of a dataset.

Not sure what you're comparing with:

if &date.<September


Super User
Posts: 19,815

Re: Condtional renaming or choosing of a dataset.

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;

Ask a Question
Discussion stats
  • 5 replies
  • 210 views
  • 0 likes
  • 4 in conversation