Thursday, June 23, 2016

SAS: Negative times

If a time value is negative, it could lose the sign depending on the format you choose... Format timew. assumes time, whereas todw. assumes datetime.

data _null_ ;
format __TIME TIME10. ;
minutes = -23;
__TIME = minutes * 60;
format __TOD10 TOD10. ;
__TOD10 = minutes * 60;
__IN_MINUTES = intck( "MINUTES" , 0 , __TIME ) ;
put _all_ ;
run;

Log

1    data _null_ ;
2       format __TIME TIME10. ;
3       minutes = -23;
4       __TIME = minutes * 60;
5       format __TOD10 TOD10. ;
6       __TOD10 = minutes * 60;
7       __IN_MINUTES = intck( "MINUTES" , 0 , __TIME ) ;
8       put _all_ ;
9    run;

__TIME=-0:23:00 minutes=-23 __TOD10=23:37:00 __IN_MINUTES=-23 _ERROR_=0 _N_=1
NOTE: DATA statement used (Total process time):
real time           0.05 seconds
cpu time            0.04 seconds

Tuesday, June 21, 2016

SAS: Looping through an array with DO OVER

A “legacy” programmer can get the job done, but not always in the most efficient way.

No one ever sends the “legacy” guys to training.

Hence this blog, as I attempt to record new (to me) tricks this old dog has learned.

Here’s one: using “do over” to loop through a SAS array.

Source code:

* demonstrate do over for looping through array ;

data work.my_array;
prime1 = 2; prime2 = 3; prime3 = 5; prime4 = 7; prime5 = 11;
run;

data _null_;
set work.my_array;
array primes prime1-prime5;
sum = 0;
do over primes;
put primes=;
sum = sum + primes;
end;
put sum=;
run;

Log:

1    * demonstrate do over for looping through array ;
2
3    data work.my_array;
4    prime1 = 2; prime2 = 3; prime3 = 5; prime4 = 7; prime5 = 11;
5    run;

NOTE: The data set WORK.MY_ARRAY has 1 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time           0.04 seconds
cpu time            0.04 seconds

6
7    data _null_;
8    set work.my_array;
9    array primes prime1-prime5;
10   sum = 0;
11   do over primes;
12       put primes=;
13       sum = sum + primes;
14   end;
15   put sum=;
16   run;

prime1=2
prime2=3
prime3=5
prime4=7
prime5=11
sum=28
NOTE: There were 1 observations read from the data set WORK.MY_ARRAY.
NOTE: DATA statement used (Total process time):
real time           0.05 seconds
cpu time            0.06 seconds

SAS: option varinitchk

One thing I like about Java is that it is a tightly-typed language. In other words, every variable must be declared with a name and a type before using.

I wish SAS was tightly typed.  How many times have we been burned by “variable is uninitialized” (which all too often means you were simply inconsistent in your spelling.)

Frustrated by my most recent error with time zones, I went looking for some way to prevent that from happening again.

I found option varinitchk, which allows you to specify how the “uninitialized” error should be handled.  There are four settings: nonote, note, warn, and error.

It may be old to some, but it is new to me.  In the future, I will be using option varinitchk=error in all of my programs.  Personal choice.

Here’s some source code to demonstrate:

* demo option varinitchk ;

options varinitchk=note;  * the default ;
data work.step1;
b = a + 1;
run;

options varinitchk=warn;
data work.step2;
b = a + 1;
run;

options varinitchk=error;
data work.step3;
b = a + 1;
run;

And here’s the log:

1    * demo option varinitchk ;
2
3    options varinitchk=note;  * the default ;
4    data work.step1;
5    b = a + 1;
6    run;

NOTE: Variable a is uninitialized.
NOTE: Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line):(Column).
1 at 5:7
NOTE: The data set WORK.STEP1 has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time           0.05 seconds
cpu time            0.04 seconds

7
8    options varinitchk=warn;
9    data work.step2;
10   b = a + 1;
11   run;

WARNING: Variable a is uninitialized.
NOTE: Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line):(Column).
1 at 10:7
NOTE: The data set WORK.STEP2 has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time           0.03 seconds
cpu time            0.03 seconds

12
13   options varinitchk=error;
14   data work.step3;
15   b = a + 1;
16   run;

ERROR: Variable a is uninitialized.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.STEP3 may be incomplete.  When this step was stopped there were 0
observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time           0.02 seconds
cpu time            0.03 seconds