220 likes | 466 Views
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_.
E N D
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_ • 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.
SAS macro %_COUNT_ • Counting values of variables
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) [... (...) [... (...)]] [/...=... (...) [... (...)]] [/...].
SAS macro %_COUNT_ • SPSS solution with COUNT (2) • in which a variable_list consists of: • variables names | implied consecutive • variable list: variable_1 TO variable_N; • and in which a value_list consists of: • separate either numeric or character • values | range(s) of values with THRU, • including LO and HI for one-sidedness.
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!@#$") .
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!@#$")) ;
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!@#$").));
SAS macro %_COUNT_ • SAS alternative functionality (3) • Syntax conversion from SPSS to SAS: • SAS macro %_Count_ supports the same • syntax and functionality as the SPSS • command COUNT. Keywords start and • end with _underscores_ and the concept • of missing values differs between SPSS • and SAS, but that is not relevant.
SAS macro %_COUNT_ • SAS alternative functionality (4) • Comparable features of macro %_Count_ • full syntax and functionality support; • any constant character value text; • missing value support with _SYSMIS_; • support of implied, consecutive, same type variable lists: _TO_-convention; • f. support of (numeric) value ranges: _THRU_-convention; _LO_ and _HI_.
SAS macro %_COUNT_ • SAS additional functionality • Extraneous features of macro %_Count_: • lexicographic comparison of character values with the _THRU_ keyword; • variables in value (range) lists (no TO) • constant values in variable list (no TO) • array elements as variables, except with an implied list (_TO_-convention) • SAS name literals instead of variables.
SAS macro %_COUNT_ • SAS use of macro %_Count_ • Macro %_Count_ call within data step: • 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; • newer versions will contain some bells and whistles as additional arguments.
Q&A: SAS macro %_COUNT_ • QUESTIONS • & • ANSWERS • SASquestions@ocs-consulting.com • Jim.Groeneveld@ocs-biometricsupport.com • http://home.hccnet.nl/jim.groeneveld/count
Q&A: SAS macro %_COUNT_ • Origin of SAS macro %_Count_ • SPSS experience from 1975 to 1997; • mainframe versions, command language; • SPSS/PC, command language oriented; • SPSS for Windows with graphical user interface; • SPSS9toX: SPSS-9 to SPSS-X syntax converter; • SAS experience since 1997 (to date); • contributor to SAS-L (comp.soft-sys.sas); • aware of nice SPSS features lacking in SAS; • needed functionality like COUNT (and RECODE); • much experience with writing SAS macros.
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)
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_;
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 ;
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.
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=/
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.?