BookmarkSubscribeRSS Feed
ijm_wf
Fluorite | Level 6

I'm trying to create a new table called table_info using proc sql. The table will consist of three variables: var1, var2, var3 with each of the variables being the result of a select statement. How can I modify the following code? Please respond with short code examples.  Thanks.

 

 

proc sql;
create table table_info as
select count(*) as var1 from dbschema.tab1;
select count (distinct id) as var2 from dbschema.tab2;
select max (run_dt) as var3 from dbschema.tab3;
quit;

 

3 REPLIES 3
ballardw
Super User

For your exact example this should work

proc sql;
create table table_info as
  select a.var1, b.var2, c.var3
  from 
      (select count(*) as var1 from dbschema.tab1) as a,
      (select count (distinct id) as var2 from dbschema.tab2) as b,
      (select max (run_dt) as var3 from dbschema.tab3) as c
  ;
quit;

You will likely get a note about a cartesian product in the log.

If you have something in the individual tables to group the data then likely you will want some form of Join on the group by variables

PGStats
Opal | Level 21

If you want to avoid the note about cartesian join, you can do:

 

proc sql;
create table myTable (var1 num, var2 num, var3 num);
insert into myTable
set 
    var1 = (select max(weight) from sashelp.class),
    var2 = (select max(weight) from sashelp.cars),
    var3 = (select max(weight) from sashelp.heart);
quit;
PG
Ksharp
Super User
proc sql;
create table table_info as
select 
(select count(*) as var1 from dbschema.tab1 ) as var1,
(select count (distinct id) as var2 from dbschema.tab2 ) as var2,
(select max (run_dt) as var3 from dbschema.tab3 ) as var3

from dbschema.tab1(obs=1) ;
quit;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

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
  • 3 replies
  • 5833 views
  • 7 likes
  • 4 in conversation