21
1 © OCS Biometric Support SAS macro %_COUNT_ Jim Groeneveld, OCS Biometric Support, Leiden, the Netherlands. CC01 – PhUSE 2008

SAS macro %_COUNT_

  • Upload
    zuwena

  • View
    90

  • Download
    0

Embed Size (px)

DESCRIPTION

SAS macro %_COUNT_. Jim Groeneveld, OCS Biometric Support, Leiden, the Netherlands. CC01 – PhUSE 2008. SAS macro %_COUNT_. AGENDA / CONTENTS Purpose: stating the problem SPSS solution with COUNT SAS alternative functionality SAS additional functionality. SAS macro %_COUNT_. - PowerPoint PPT Presentation

Citation preview

Page 1: SAS macro %_COUNT_

1© OCS Biometric Support

SAS macro %_COUNT_

Jim Groeneveld,

OCS Biometric Support,

Leiden, the Netherlands.

CC01 – PhUSE 2008

Page 2: SAS macro %_COUNT_

2© OCS Biometric Support

SAS macro %_COUNT_

AGENDA / CONTENTS

1. Purpose: stating the problem

2. SPSS solution with COUNT

3. SAS alternative functionality

4. SAS additional functionality

Page 3: SAS macro %_COUNT_

3© OCS Biometric Support

SAS macro %_COUNT_

PURPOSE: stating the problem

Counting the occurrence of many specific

values of specified variables in SAS data

step takes quite a lot of coding work.

Macro %_COUNT_ facilitates the process.

Page 4: SAS macro %_COUNT_

4© OCS Biometric Support

SAS macro %_COUNT_

Counting values of variables

How often do certain numerical

or character

values occur in specific

variables within

records?

Var1 Var2 Var3 Var4 Count4,5,6

1 2 3 4 1

5 6 7 8 2

VarA VarB VarC VarD Count'b'-'f'

a b c d 3

e f g h 2

Page 5: SAS macro %_COUNT_

5© OCS Biometric Support

SAS macro %_COUNT_

SPSS solution with COUNT (1)

SPSS has the command COUNT, syntax:

• simple form (example):COUNT Countvar = var_a, var_b [...] (value_1, value_2, value_3 [...]).

• general form:COUNT CountVar = variable_list (value_list) [... (...) [... (...)]] [/...=... (...) [... (...)]] [/...].

Page 6: SAS macro %_COUNT_

6© OCS Biometric Support

SAS macro %_COUNT_

SPSS solution with COUNT (2)

in which a variable_list consists of:variables names | implied consecutivevariable list: variable_1 TO variable_N;

and in which a value_list consists of:separate either numeric or charactervalues | range(s) of values with THRU,including LO and HI for one-sidedness.

Page 7: SAS macro %_COUNT_

7© OCS Biometric Support

SAS macro %_COUNT_

SPSS solution with COUNT (3)

SPSS COUNT, complex example:

COUNT

Counter1 = NumA, NumB (0, MISSING) /

Counter2 = NumC, NumD, NumE TO NumI

(3, 5 THRU 9, 14 THRU HI, SYSMIS)

CharA, CharB, CharC TO CharH, CharI

('a text', 'b text', "any!@#$") .

Page 8: SAS macro %_COUNT_

8© OCS Biometric Support

SAS macro %_COUNT_

SAS alternative functionality (1)

(approx.) equivalent SAS only code:

Counter1 = (NumA=0) + MISSING(NumA) + (NumB=0) + MISSING(NumB);

Counter2 = (NumC EQ 3) + (NumC>=5 AND NumC<=9) + (NumC>=14) + MISSING(NumC)

+ (NumD EQ 3) + (NumD>=5 AND NumD<=9) + (NumD>=14) + MISSING(NumD)

+ (NumE EQ 3) + (NumE>=5 AND NumE<=9) + (NumE>=14) + MISSING(NumE)

+ (NumF EQ 3) + (NumF>=5 AND NumF<=9) + (NumF>=14) + MISSING(NumF)

+ (NumG EQ 3) + (NumG>=5 AND NumG<=9) + (NumG>=14) + MISSING(NumG)

+ (NumH EQ 3) + (NumH>=5 AND NumH<=9) + (NumH>=14) + MISSING(NumH)

+ (NumI EQ 3) + (NumI>=5 AND NumI<=9) + (NumI>=14) + MISSING(NumI)

+ (CharA IN('a text', 'b text', "any!@#$"))

+ (CharB IN('a text', 'b text', "any!@#$"))

+ (CharC IN('a text', 'b text', "any!@#$"))

+ (CharD IN('a text', 'b text', "any!@#$"))

+ (CharE IN('a text', 'b text', "any!@#$"))

+ (CharF IN('a text', 'b text', "any!@#$"))

+ (CharG IN('a text', 'b text', "any!@#$"))

+ (CharH IN('a text', 'b text', "any!@#$"))

+ (CharI IN('a text', 'b text', "any!@#$")) ;

Page 9: SAS macro %_COUNT_

9© OCS Biometric Support

SAS macro %_COUNT_

SAS alternative functionality (2)

(approx.) equivalent %_Count_ code:

%_COUNT_ ((

Counter1 = NumA, NumB (0, _SYSMIS_) /

Counter2 = NumC NumD NumE _TO_ NumI

(3 5 _THRU_ 9 14 _THRU_ _HI_ _SYSMIS_)

CharA, CharB, CharC _TO_ CharH, CharI

('a text', 'b text', "any!@#$").));

Page 10: SAS macro %_COUNT_

10© OCS Biometric Support

SAS macro %_COUNT_

SAS alternative functionality (3)

Syntax conversion from SPSS to SAS:

SAS macro %_Count_ supports the samesyntax and functionality as the SPSScommand COUNT. Keywords start andend with _underscores_ and the conceptof missing values differs between SPSSand SAS, but that is not relevant.

Page 11: SAS macro %_COUNT_

11© OCS Biometric Support

SAS macro %_COUNT_

SAS alternative functionality (4)

Comparable features of macro %_Count_ a. full syntax and functionality support;b. any constant character value text;c. missing value support with _SYSMIS_;d. support of implied, consecutive, same

type variable lists: _TO_-convention;e. f. support of (numeric) value ranges:

_THRU_-convention; _LO_ and _HI_.

Page 12: SAS macro %_COUNT_

12© OCS Biometric Support

SAS macro %_COUNT_

SAS additional functionality

Extraneous features of macro %_Count_: a. lexicographic comparison of character

values with the _THRU_ keyword;b. variables in value (range) lists (no TO)c. constant values in variable list (no TO)d. array elements as variables, except

with an implied list (_TO_-convention)e. SAS name literals instead of variables.

Page 13: SAS macro %_COUNT_

13© OCS Biometric Support

SAS macro %_COUNT_

SAS use of macro %_Count_

Macro %_Count_ call within data step:1. SPSS-like code between double left

and right parentheses: one set to start the macro and the other set to contain the whole SPSS-like code as one value for the main, first, positional macro argument that is parsed by the macro;

2. newer versions will contain some bells and whistles as additional arguments.

Page 14: SAS macro %_COUNT_

14© OCS Biometric Support

Q&A: SAS macro %_COUNT_

QUESTIONS&

ANSWERS

[email protected]

[email protected]

http://home.hccnet.nl/jim.groeneveld/count

Page 15: SAS macro %_COUNT_

15© OCS Biometric Support

Q&A: SAS macro %_COUNT_

Origin of SAS macro %_Count_

1. SPSS experience from 1975 to 1997;a. mainframe versions, command language;b. SPSS/PC, command language oriented;c. SPSS for Windows with graphical user interface;d. SPSS9toX: SPSS-9 to SPSS-X syntax converter;

2. SAS experience since 1997 (to date);a. contributor to SAS-L (comp.soft-sys.sas);b. aware of nice SPSS features lacking in SAS;c. needed functionality like COUNT (and RECODE);d. much experience with writing SAS macros.

Page 16: SAS macro %_COUNT_

16© OCS Biometric Support

Q&A: SAS macro %_COUNT_

Comparison of SPSS and SAS missings

SPSS – numeric and short strings:• user defined: any values per variable

(up to three, or one plus a range)• system missing: automatically assigned

(just one) (not applicable to strings)

SAS - only fixed values:• character: space• numeric: ._ . .a to .z (28 ones in order)

Page 17: SAS macro %_COUNT_

17© OCS Biometric Support

Q&A: SAS macro %_COUNT_

Algorithm of _TO_-convention

Hardly or not at all possible to let the macro know the implied variable names

PROC CONTENTS, VARNUM and VARNAME

Macro does not need to know var names:• %LET Incr = %IncrIndx; %* GLOBAL;• ARRAY _0&Incr StartVar EndVar;• DO _0_ = 1 TO DIM(_0&Incr); DROP _0_;

Page 18: SAS macro %_COUNT_

18© OCS Biometric Support

Q&A: SAS macro %_COUNT_

SAS name literal

A SAS name literal is a name token that is expressed as a string within quotation marks, followed by the letter n. For more information about SAS name literals, see SAS Language Reference: Concepts.

Example:'This !@#$%^&* value'n = 0 ;

Page 19: SAS macro %_COUNT_

19© OCS Biometric Support

Q&A: SAS macro %_COUNT_

Conversion Demonstration

To see the generated, converted SAS code of a call to the macro %_Count_ (many macros actually) run the code:

%PUT {Start of generated code} %QUOTE(

/* the complete %_Count_ call here */

) {End of generated code} ;

All (unconditionally) generated SAS code will appear in the log, not neatly formatted, but logically working.

Page 20: SAS macro %_COUNT_

20© OCS Biometric Support

Q&A: SAS macro %_COUNT_

Newer version's extra features

additional user specifiable arguments:GlobIncr=_ArrayNr /* name of unique global incr. macro variable */,

ArPrefix=_0 /* prefix of unique temporary SAS array for TO-list */,

IndexVar=_0_ /* name of unique index variable for array elements */,

Sysmis=_SYSMIS_, To=_TO_, Thru=_THRU_,

Lo=_LO_, Hi=_HI_, Assign==, NewCount=/

Page 21: SAS macro %_COUNT_

21© OCS Biometric Support

Q&A: SAS macro %_COUNT_

Additional unintended features

• Expressions without spaces, with +-*involving constant and variable values:

Counter21 = a, b c (3, -5+7)/* expression with constant values */

/ Counter22 = b, a b (c-a, 9)/* expression with variables */

/ Counter23 = a+c (2*2) /* expression in variable list as well */

Not at all guaranteed! No div., log. expr.?