Tips on SAS
Posted on Oct 10, 2013 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. **
-
SAS on Linux recognize
~
as the home directory of users. -
good to use a profile (containing database use and password, etc.) and %include it ... e.g., .sas_profile
%include "~/.sas_profile";
Library
-
There are at least two ways to get the path of the temporary
work
library. The first way is to right click on thework
library and chooseProperty ...
. The second way is to run the following code.%put %sysfunc(getoption(work));
However, the path returned by these 2 ways might not be correct (which is really annoying). If you want to keep datasets in the "work" library, you can copy it to another permanent library. To do this,
1. select datasets in the "work" library, 2. right click and select copy 3. right click on the permanent library 4. choose paste.
Or you can use the
datasets
procedure directly.proc datasets library=source; copy out=dest; run;
-
Do not use the "work" library to save important datasets. Always use a (permanent) library.
-
SAS is case-insensitive. It does not matter whether you use upper or lower case for keywords, functions, data set names, etc. in SAS. However, strings/characters are case-sensitive. For example,
"good"
and"GOOD"
are different as characters. This matters when your SAS code relies on string comparisons. A file path is case-insensitive in Windows but case-sensitive in Linux,
so a file path in SAS running on a Windows server is case-insensitive but case-sensitive in SAS running on a Linux server. A tricky situation where case matters is when a macro variable is embedded in double quotes to be used it as a string value.
procedures
-
metalib (for user name, password information and so on) LIBNAME CRE_DATA BASE "/var/userdata/CPRA/Users/ub66536” you have additional key word base (this fixes a SAS stored process problem, if you cannot run a stored process, maybe permission)
-
lag and dif functions very useful ... can we combine lag and diff? yes lag does not work on variables that don't exist in the table so you cannot use lag on new variables instead, you should use retain
-
termstr, according to files created on different operating systems ... use different termstr
Other tips
-
dm statement is the display manager ...
-
It is very slow to display results in HTML format when there are massive results. Instead of using the prin procedure, it might be much faster to just rerun the code.
-
The
pwencode
procedure encodes password and let you use it in place of plaintext passwords in SAS programs that access relational database management systems (RDBSMs) and various servers.
Options
- The global key
options
in SAS has an aliasoption
. However, it is suggested that you always useoptions
instead ofoption
when setting SAS system options.sas proc sql stimer option; options macrogen symbolgen; option mprint symbolgen;
Syntax
-
be careful when you copy sas code from a non-text editor (e.g., MS Word). It might screw up special symbols, such as the double quotes, which results in syntax error.
-
SAS has many pre-mature syntax sugars. For example,
data _null_; x = intnx('week', '17oct03'd, 6); put x= date9.; run;
However, it is suggested that you use the following way instead as it is more consistent with other programming languages.
data _null_; x = intnx('week', '17oct03'd, 6); put 'x = ' x date9.; run;
-
You can use l <= x <= u conditions in a SAS where statement, which is more convenient. We can also do this in python.
-
The keyword descending in the sort procedure must be before the variable it describes in proc sort.
Naming
-
Names of engines, filerefs, librefs and passwords can be at most 8 characters; names of call routines, functions and procedures can be at most 16 characters; all other variable names can have at least 28 characters.
-
You can use _SomeVar as macro variable names. It seems that some people like to use this naming convention. I think this is to avoid name confliction. To avoid name confliction, a good way is to use _ followed by a sequence of random digits (e.g., _2893478219903).
Random
-
ranuni, call ranuni, unifrom, call randgen use a negative seed to use time as seed, which is recommended
-
&&x&i.
,&&x.&i.
.
is not makeing things clear, from this view, you know which is right!
branch
- only the first statment following an if ... then ... clause is run. If you want to run run multiple statements in a if ... then ... branch, you have to include them in a do ... end block.
References
http://www.jiangtanghu.com/blog/2012/10/16/incorporate-sasiml-to-base-sas/
http://support.sas.com/documentation/cdl/en/imlug/64248/HTML/default/viewer.htm#imlug_procs_sect005.htm