SAS SQL Tips
Posted on Jun 28, 2014 in Computer Science
Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.
** Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives. **
- A const column does not work if no row is selected.
This becomes trickier if you use
count(*)
in your query. For example, if the returned value ofn
in the following query is 0, thenyyyymm
is missing instead of the value201311
.
proc sql;
select
'201311' as yyyymm,
count(*) as n
from
lib.tran_201311
where tran_date >= '1Jan2014'd;
quit;
-
SAS can map a database on a data platform (e.g., Teradata) as a library. This is only suggested when you want to download data from or upload data to the database. Don't manipulate data directly in library mapping to a database. Also you cannot save a sorted data set in the library, as SQL stories data differently than SAS. If you use the print procedure to print a previously sorted data in the database, you will probably still get an unsorted data set.
-
You can use multiple SQL statements in one sql procedure (before the quite statement).
-
While records in a relational database management system (RDMS) does not perserve the order that they were created (i.e., the order of records returned by a query (without sorting) is not necessarily the same as the order of them being created), a dataset in SAS preserves the order of records created. Even if you use a SQL procedure to insert records into a dataset in SAS, the order of records is preserved.
-
Most SAS functions can be used in the SQL procedure, e.g.,
intnx
, etc. -
The
pctl
function in SAS cannot be used in the SQL procedure. -
select into, sql. great! and can be one variable
separated by " "
. -
The noprint option of the SQL procedure supress displaying of results returned by select clauses. However, if you create a table SAS Enterprise Guide will still show the dataset created.
-
When you select values into macro variables using a sql procedure in a macro function, the macro varibles created are local to the macro function.
-
In many procedures of SAS, you can use the option
obs
(together withfirstObs
) to select/limit records to work on or return. Forproc sql
, you can use the optionoutObs
to limit the number of records to display/return.
proc sql outobs=12;
select
city,
(avgHigh - 32) * 5/9 as highC format=5.1,
(avgLow - 32) * 5/9 as lowC format=5.1,
(CALCULATED HighC - CALCULATED lowC) as range format=4.1
from
worldTemps
;
quit;
- You can use an column alias in
where
,having
,on
clauses directly. However, if you want to use an column alias to create another column in aselect
statement, you have to use theCALCULATED
keywords to letproc sql
know that it is an alias.
proc sql;
select
city,
(avgHigh - 32) * 5/9 as highC format=5.1,
(avgLow - 32) * 5/9 as lowC format=5.1,
(CALCULATED highC - CALCULATED lowC) as range format=4.1
from
worldTemps
;
quit;
- union all (keep all rows) vs union (distinct/unique) prefer to use union all union and union all relies on column positions another way is to use proc append which appends data by matching column names
append as advantages over sql union append tries to match column names while sql union relies on the column orders.
be careful when you use append, be sure to remove previous data sets ... as you might run the same piece of code multiple times
-
If you sort observations using the
order by
clause when creating a table (using thecreate table
stateemnt), then observations in the created table is sorted. It is equivalent to sort a table using theproc sort
procedure. -
Even if observations in a table is sorted, you have to rely on the
order by
clause to get sorted observations. That is theselect
statement withoutorder by
guarantees no specific order of observations. This is true for all versions of SQLs. -
When creating a new table/view using the SQL procedure, you can specify columns to keep/drop similarly as you can do in other procedures.
data s;
input x $10. y;
datalines;
1234567890 2
3 4
5 6
;
run;
proc sql;
create table s2 (keep=y) as
select
*
from
s
;
quit;
proc sql;
create table s3 (drop=x) as
select
*
from
s
;
quit;