Search notes:

SAS: proc sql - select

Format columns

data tq84_data;
  do i = 1 to 20;
     dt = floor(rand('norm') * 5000 + 10000);
     output;
  end;
run;

proc sql;
  select
    i,
    dt format date9.
  from
    tq84_data;
quit;

proc sql;
  select
    min(dt) as min_dt format date9.
  from
    tq84_data;
quit;
Github repository about-SAS, path: /programming/proc/sql/select/format-column.sas

Keyword calculated

The non-ANSI keyword calculated can be used to refer to a previously calucated value in select statement:
data tq84_data;
  length num_1 4.
         num_2 4.
         txt   $3;

  input num_1 num_2 txt;

datalines;
13 2 abc
7 913 def
25 40 ghi
873 51 jkl
79 13 mno
645 316 pqr
100 100 stu
run;

proc sql;
  select
    num_1 / num_2        as ratio,
    1 / calculated ratio as ratio_inv,
    txt
  from
    tq84_data
  where
    calculated ratio > 1;
quit;
Github repository about-SAS, path: /programming/proc/sql/select/calculated.sas

Where contains

contains can be used like like in ANSI SQL. contains can be abbreviated with the question mark (?):
data tq84_data;
  length num   4.
         txt $20;

  input num txt;

datalines;
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten
11 eleven
12 twelve
13 thirteen
run;

proc sql;
  select
    *
  from
    tq84_data
  where
    txt contains 've';
quit;

proc sql;
  select
    *
  from
    tq84_data
  where
    txt ? 'ee';
quit;
Github repository about-SAS, path: /programming/proc/sql/select/where/contains.sas

flow

With flow, a maximum width can be set for a column:
proc sql;

  create table work.tab (
    foo num,
    bar char(100),
    baz char(100)
  );

  insert into tab
    set foo = 1                    ,
        bar = '123456789 123456879',
        baz = '123456789 123456879';

  insert into tab
    set foo = 2                    ,
        bar = repeat('abcdef'  ,10),
        baz = repeat('ghi'     ,15);

  insert into tab
    set foo = 3                     ,
        bar = repeat('Mnopqr'  , 18),
        baz = repeat('Stuvw '  ,  6);

quit;

proc sql flow = 20;
  select 
    foo,
    bar,
    baz
  from
    work.tab;
quit;

/*
     foo  bar                   baz
----------------------------------------------------
       1  123456789 123456879   123456789 123456879 
       2  abcdefabcdefabcdefab  ghighighighighighigh
          cdefabcdefabcdefabcd  ighighighighighighig
          efabcdefabcdefabcdef  highighi            
          abcdef                                    
       3  MnopqrMnopqrMnopqr    Stuvw Stuvw Stuvw   
          MnopqrMnopqrMnopqr    Stuvw Stuvw         
          MnopqrMnopqrMnopqr    Stuvw Stuvw         
          MnopqrMnopqrMnopqr                        
          MnopqrMnopqrMnopqr                        
          MnopqrMnop         
*/
Github repository about-SAS, path: /programming/proc/sql/select/flow.sas

See also

select into
joins
proc sql

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Companie...', 1759415442, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Companies-Products/SAS/programming/proc/sql/select/index(204): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78