The Versatile Colon
5 for the price of 1! The colon has several uses in SAS, all of them worth knowing about.
1. In a “name prefix list” of variables
In contexts where a list of variables is required, the colon is a wildcard character denoting any number of characters up the end of the variable name. For example:
keep w: ; drop tmp: ;
This code will keep all variables whose names begin with “w” and drop all variables who names begin with “tmp”.
2. As an operator modifier
The colon behaves in a similar way when modifying operators used in character comparisons, for example: if string =: 's' then put 'Begins with "s"'; if string in: ('r','s','t') then put 'Begins with "r", "s" or "t"' ; if string >=: 's' then put 'Begins with "s" or a later letter' ;
3. As an input format modifier
The colon means “start reading at the next non-blank column”, for example:
data _null_; infile cards truncover; input name :$20. value :2.; cards; tom 23 dick 54 harry 9 ; run;
4. As an output format modifier
The colon means “take as much space as you need and no more”. For example:
put name :$20 value 2.;
This code strips both leading and trailing blanks from NAME, and leaves a single space before printing the numeric VALUE. It’s a bit like using the CATX function.
5. For creating a macro variable in PROC SQL
The following code creates a macro variable WDSCOUNT containing a count of data sets in the WORK library:
proc sql; select count(*) into :wdscount from dictionary.tables where libname='WORK' and memtype='DATA'; quit; data _null_; put "Currently &wdscount datasets in WORK library."; run;