Help using Base SAS procedures

SQL create table issue

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

SQL create table issue

Hi all,

The following code seems to work as expected but it loses the labels and formats that should be copied from the "updated." table. Can you suggest a method to retain these dataset attributes?

&dsName = name of the dataset being processed

&pt = subject identifier

%dsSQLcolumns = list of columns to be selected for the "except" statement.

%dsSEQk = additional columns to be added onto the final "except_" table but are not part of the "except" statement.

Please note that there is no primary key in the datasets that I can pick.

proc sql;

      create table except_&dsname

      as

      select

            %dsSQLcolumns(ds=&dsname) %dsSEQk(ds=&dsname)

      from updated.&dsname

      natural join

      (select

            %dsSQLcolumns(ds=&dsname)

      from updated.&dsname

      where usubjid="&pt"

      except

      select

            %dsSQLcolumns(ds=&dsname)

      from olddata.&dsname

      where usubjid="&pt");

quit;


Accepted Solutions
Solution
‎05-18-2013 11:55 AM
Respected Advisor
Posts: 4,641

Re: SQL create table issue

The problem is likely to be with the natural join. Run this little test :

data a(drop=b) b(drop=a);
do i = 1 to 5;
     a = i*10;
     b = i**2;
     output;
     end;
format a z10.2 b dollar6. i percent10.;
run;

proc sql;

create table c as
select i as i_in_c, a, b
from a natural join b;

select * from c;

create table d as
select a.i as i_in_d, a, b
from a natural join b;

select * from d;
quit;

Note that the only difference between table c and table d is the fact that the origin of variable i is explicit in the select statement creating table d. Even when both variables in the join have the same format, the natural join drops the format. The solution is thus to mention one of the table names for every column involved in the join.

PG

PG

View solution in original post


All Replies
Solution
‎05-18-2013 11:55 AM
Respected Advisor
Posts: 4,641

Re: SQL create table issue

The problem is likely to be with the natural join. Run this little test :

data a(drop=b) b(drop=a);
do i = 1 to 5;
     a = i*10;
     b = i**2;
     output;
     end;
format a z10.2 b dollar6. i percent10.;
run;

proc sql;

create table c as
select i as i_in_c, a, b
from a natural join b;

select * from c;

create table d as
select a.i as i_in_d, a, b
from a natural join b;

select * from d;
quit;

Note that the only difference between table c and table d is the fact that the origin of variable i is explicit in the select statement creating table d. Even when both variables in the join have the same format, the natural join drops the format. The solution is thus to mention one of the table names for every column involved in the join.

PG

PG
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 165 views
  • 0 likes
  • 2 in conversation