As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Numeric transformations

From sasCommunity
Jump to: navigation, search

Here are SAS Expressions for some handy numeric transformations. Some are simple and obvious, others less so.

To start, here are a few which preserve magnitude.

                                    .     .M  -43.2     -1      0      1   23.4
                                -----  -----  -----  -----  -----  -----  -----

Given
 x                                  .      M  -43.2     -1      0      1   23.4

Negation
 - x                                .      .   43.2      1      0     -1  -23.4

Absolute value
 abs(x)                             .      .   43.2      1      0      1   23.4

Zero in place of missing
 sum(x,0)                           0      0  -43.2     -1      0      1   23.4

Others map into finite sets of possible values. That makes them convenient for use in branching structures (SELECT in the DATA step, CASE in SQL). In some cases the result is boolean (true/false).

                                    .     .M  -43.2     -1      0      1   23.4
                                -----  -----  -----  -----  -----  -----  -----

Remove magnitude
 sign(x)                            .      .     -1     -1      0      1      1

Missingness
 missing(x)                         1      1      0      0      0      0      0

Divisor suitability
 x NE 0                             1      1      1      1      0      1      1

False
     not x                          1      1      0      0      1      0      0

True
 not not x                          0      0      1      1      0      1      1

Positive
 sign(x) EQ       1                 0      0      0      0      0      1      1

Non-negative
 sign(x) IN (   0,1)                0      0      0      0      1      1      1

Negative
 sign(x) EQ  -1                     0      0      1      1      0      0      0

Non-positive
 sign(x) IN (-1,0)                  0      0      1      1      1      0      0

Here are alternative versions of the last six. They propagate missing values.

                                    .     .M  -43.2     -1      0      1   23.4
                                -----  -----  -----  -----  -----  -----  -----

False (3-branch)
  1 - abs( sign(x) )                .      .      0      0      1      0      0

True (3-branch)
      abs( sign(x) )                .      .      1      1      0      1      1

Positive (3-branch)
 1 + sign( sign(x) - 1 )            .      .      0      0      0      1      1

Non-negative (3-branch)
     sign( sign(x) + 1 )            .      .      0      0      1      1      1

Negative (3-branch)
 1 - sign( sign(x) + 1 )            .      .      1      1      0      0      0

Non-positive (3-branch)
   - sign( sign(x) - 1 )            .      .      1      1      1      0      0

If it's necessary to propagate special missing values, it's more complicated. For example, here is the expression which negates non-missing values but propagates specific missing values (special or not).

                                    .     .M  -43.2     -1      0      1   23.4
                                -----  -----  -----  -----  -----  -----  -----


Negation (special)
 ifn(missing(x),x,-x)               .      M   43.2      1      0     -1  -23.4

Code