Tuesday, February 25, 2014

SAS: Change location of WORK folder

This is something I always forget how to do, and have to relearn it for failure of committing it to a place like this.  Found this in an old email and repeating it here.  (Your path may differ.)


To change the location of the work files, edit E:\SAS\Config\Lev1\SASApp\sasv9_usermods.cfg.  You should see something like this:

/* Setup the default SAS System user work folder */
-WORK “J:\SASWORK”

Note there is no semi-colon on the –WORK line.


Updated 20140226.

Also check C:\Program Files\SASHome\SASFoundation\9.4\nls\en\sasv9.cfg
In this case I tried making a sasv9_usermods.cfg but it appeared to be ignored.
Always a good idea to make a backup file first as I did here:



You can check by running PROC OPTIONS and find "WORK=" in the log.


Sunday, January 5, 2014

SAS: Force day of week

I had to do some data extracts, but wanted to make sure I got an entire week of data on the front end and back end. Client's fiscal week begins on Thursday. So for a given date, how do I find beginning of fiscal week and ending of fiscal week? Here's what I did as a test program:

* Find fiscal week begin date and week ends date. ;
* Fiscal week always begins on Thursday and ends on Wednesday. ;

data _null_;
format date week_begins week_ends date9.;
date = '04Jan2014'd;
put date=;
week_begins = date - weekday(date) + 5;
week_begins_day = weekday(week_begins);
put week_begins= week_begins_day=;
week_ends = week_begins + 6;
week_ends_day = weekday(week_begins);
put week_ends= week_ends_day=;
run;



Log:

date=04JAN2014
week_begins=02JAN2014 week_begins_day=5
week_ends=08JAN2014 week_ends_day=4



(Recall in SAS, weekday function returns 1 for Sunday, 2 for Monday, ... , 7 for Saturday.)




Wednesday, December 18, 2013

SAS: Conditional %include

I have a SAS program which is generating SAS programs, and I want to conditionally use %include; that is, if the %include file exists, then %include it.  This code demonstrates how.

* SAS conditional %include ;
* See also http://www.sascommunity.org/wiki/Conditionally_Executing_Global_Statements ;

data _null_;
file "c:\temp\a.txt";
put "a = 1;";
run;

data _null_;
file "c:\temp\b.txt";
put "b = 1;";
run;

%let include_file=c:\temp\a.txt;
%let include_file=c:\temp\c.txt;
%let include_file=c:\temp\b.txt;

data Work.test;
%sysfunc(ifc(%sysfunc(fileexist("&include_file")),
    %include "&include_file";, %put File "&include_file" not found.;));
run;

proc print data=Work.test;
run;


Saturday, December 7, 2013

SAS: Looping thru a macro variable

Source code:

%macro doit(FOREACH=);
%local i j ;
%let j = 1 ;
%do %while(%scan(&FOREACH,&j) ne ) ;
   %let i = %scan(&FOREACH,&j) ; 

   %put &FOREACH &j &i;
   %let j = %eval(&j+1) ;
%end ;
%mend doit;

%doit();                 * none ;
%doit(FOREACH=A);        * one only, no parenthesis ;
%doit(FOREACH=(B));      * one only, with parenthesis ;
%doit(FOREACH=(C,D));    * two, comma separated, with parenthesis ;
%doit(FOREACH=(E F G));  * three, space separated, with parenthesis ;



Output from log:


125  %macro doit(FOREACH=);
126  %local i j ;
127  %let j = 1 ;
128  %do %while(%scan(&FOREACH,&j) ne ) ;
129     %let i = %scan(&FOREACH,&j) 

130     %put &FOREACH &j &i;
131     %let j = %eval(&j+1) ;
132  %end ;
133  %mend doit;
134
135  %doit();                 * none ;
136  %doit(FOREACH=A);        * one only, no parenthesis ;
A 1 A
137  %doit(FOREACH=(B));      * one only, with parenthesis ;
(B) 1 B
138  %doit(FOREACH=(C,D));    * two, comma separated, with parenthesis ;
(C,D) 1 C
(C,D) 2 D
139  %doit(FOREACH=(E F G));  * three, space separated, with parenthesis ;
(E F G) 1 E
(E F G) 2 F
(E F G) 3 G



Friday, December 6, 2013

SAS: Floating point hell

I grew up on COBOL and mainframe Assembler.  I loved packed decimal numbers because you always knew what you had.  I HATE floating point numbers.  I first stumbled upon this issue accidentally while learning C.  The same issue exists in Java.  And SAS. 

I don't have time to elaborate right now.  So I just want to capture the problem.  Here is the SAS log.  compare variables (e and f), (c and h), and (b and g).  In particular, note my use of +1.E-10. Warning ... +1.E-11 gives different results.

1    data _null_;
2    a = 400.4444;
3    i = 4;
4    b = int(a * 10 ** i) / (10 ** i);
5    c = (a = b);
6    d = a * 10 ** i;
7    e = int(a * 10 ** i);
8    f = int(a * 10 ** i + 1.E-10);
9    g = int(a * 10 ** i + 1.E-10) / (10 ** i);
10   h = (a = g);
11   put a= i= b= c= d= e= f= g= h=;
12   run;

a=400.4444 i=4 b=400.4443 c=0 d=4004444 e=4004443 f=4004444 g=400.4444 h=1