0% found this document useful (0 votes)
2 views5 pages

Understanding SAS Colon Usage

Uploaded by

Akash Raj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Understanding SAS Colon Usage

Uploaded by

Akash Raj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Coders' Corner

Paper 73-26

That Mysterious Colon (:)


Haiping Luo, Dept. of Veterans Affairs, Washington, DC

when x is not equal to y. The statement ‘return;’ brings the


execution back to the beginning of the data step for the next
ABSTRACT observation, without executing the two statements after the ‘yes:’
The colon (:) plays certain roles in SAS coding. Its usage, label. Only when the condition 'x=y' is met, will the program jump
however, is not well documented nor is it clearly indexed in SAS to the label ‘yes:’ and execute the two statements which follow the
manuals. This paper shows how a colon can be used as a label label. The value of x and z will be printed to the log, the
indicator, an operator modifier, a format modifier, a key word observation will be deleted and the then the program will read in
component, a variable name wildcard, an array bound delimiter, the next observation.
an argument feature delimiter, a special log indicator, or an index
creation operator. Mastering these usages can give your code
Similarly, a LINK statement also branches execution to
needed functionality and/or an efficiency lift.
statements after a label. The difference between the GOTO and
the LINK statements is that after the execution the code following
AN OVERVIEW the label (the labeled statement group), a ‘return;’ statement in a
In SAS language, the colon (:) has many different uses, although LINK structure will bring execution to the statement following the
they are not well documented. It is difficult to search for the LINK statement, while a ‘return;’ in a GOTO structure will bring
colon’s usage in SAS OnlineDoc, System Help, and printed execution to the beginning of the data step. The use of label in a
manuals. From the scattered documentations, publications, and LINK structure can be seen in the following example:
featured programmers, this paper collected nine types of colon data workers;
usages: set tickets; by ssn;
1. Label indicator if [Link] then link init;
2. Format modifier tickets+1;
3. Operator modifier tothrs+hours;
4. Key word component if [Link] then output;
5. Variable name wildcard return;
6. Array bound delimiter init:
7. Argument feature delimiter tickets=0;
8. Special log indicator tothrs=0;
9. Index creation operator return;

Some of these usages can improve coding efficiency while others This data step sums tickets and total hours for each worker in the
provide unique and necessary capacities for various dataset ‘tickets’ and outputs the sums to dataset ‘workers’. The
circumstances. This paper will use examples to demonstrate statements after the label ‘init’ are only executed for the first
different ways in which the colon can be used. Most of the observation of a social security number. After execution of the
examples in this paper were tested under SAS for Windows labeled statement group, execution continues to ‘tickets+1’ for the
Version 8. Some of them were tested under version 6.12 and same [Link] observation since this is the statement immediately
found not working. These known invalid cases are indicated in the following the LINK statement. If GO To were used instead of
text. LINK, execution would cause immediate reading of the next
observation without incrementing ‘tickets’ and ‘tothrs’ with data
from the first observation for this worker.
1. LABEL INDICATOR
A colon after a string signals the string as a label and the
Label can be used in report writing to execute statements when a
statement(s) after the colon as the labeled statement(s). The label
new report page is begun. The following code uses a data step to
string must be a valid SAS name. Any statement(s) within a data
write a customized report with a HEADER=label option in a file
step can be labeled, although no two labels in a data step can
statement:
have the same name. The statement label identifies the
data _null_;
destination of a GO TO statement, a LINK statement, the
HEADER= option in a FILE statement, or the EOF= option in an set sales;
INFILE statement. by dept;
/* header refers to the statements after the
The labeled statement referred to by a GO TO or a LINK label newpage */
statement is used to alter the sequential flow of program file print header=newpage;
execution. For example, in the following code: /*Start a new page for each department:*/
data a; if [Link] then put _page_;
input x y z; put @22 salesrep @34 salesamt;
if x=y then go to yes; return;
x=10; /* the put statement is executed for each
y=32; new page */
return; newpage:
yes: put @20 'Sales for 2000' /
put x= z=; @20 dept=;
delete; return;
cards; run;
. . .
Label can also be used in the EOF=label option of an INFILE
statements 'x=10;' 'y=32;' will be executed for all observations statement to indicate how execution is to proceed after the end of
Coders' Corner

a file is reached. In the following code, suppose [Link] has 3 for ‘format/informat modifier’ instead of ‘colon’; alternatively, you
observations while [Link] has 6. Without EOF=, the input find examples by getting lucky. For input, colon enables you to
statement will stop reading at observation 3 when it reaches the use an informat for reading a data value in an otherwise list input
end of [Link]; dataset a will have only 3 observations and 6 process. The colon informat modifier indicates that the value is to
variables. With EOF=more and the label ‘more:’, the data step be read from the next nonblank column until the pointer reaches
continues, so the remaining 3 observations in [Link] are read the next blank column or the end of the data line, whichever
by the second input statement: dataset a will have 6 observations comes first. Though the data step continues reading until it
and 6 variables. reaches the next blank column, it truncates the value of a
data a; character variable if the field is longer that its formatted length. If
infile "d:\[Link]" eof=more ; the length of the variable has not been previously defined, its
input @1 name $20. @21 x 5.1 @26 y 8.; value is read and stored with the informat length.
more:
infile "d:\[Link]"; In the following case, the input data are not lined up neatly. Some
input @1 book $20. @21 test 8.1 @29 st 8.; of the problems are: the first data line does not start in column 1;
run; there are single spaces and a semi-colon embedded in character
values; the value of ‘city’ in the second data line starts in column
12, which is within the defined range of the first variable; and
In SAS Macro Language, there is a %GOTO statement that there are 2 or more spaces between variables. Given these
branches macro execution to a labeled section within the same features of the data, if no colon is used, the code will produce the
macro. Branching with the %GOTO statement has two output printed at the end of the code:
restrictions. First, the label that is the target of the %GOTO /* Without colon in the input statement*/
statement must exist in the current macro. Second, a %GOTO
data a;
statement cannot cause execution to branch to a point inside an
iterative %DO, %DO %UNTIL, or %DO %WHILE loop that is not input student $14. city & $30.;
currently executing. The following example uses %GOTO to exit cards4;
the macro when a specific type of error occurs: Jim Smith Washington DC; L.A.
%macro check(parm); Key Jones Chicago
%local status; ;;;;
%if &parm= %then %do; proc print;
%put ERROR: You must supply a run;
parameter to macro CHECK.;
%goto exit; The output missed the starting character of the variables
%end; OBS STUDENT CITY
more macro statements that test for 1 Jim Smith
error conditions . . . 2 Key Jones Chi cago
%if &status > 0 %then %do;
%put ERROR: File is empty.; If, however, a colon is added to the input statement:
%goto exit; input student : $14. city & $30.;
%end;
more macro statements . . . The output becomes:
%put Check completed successfully.; OBS STUDENT CITY
%exit: 1 Jim Smith
%mend check; 2 Key Jones

In SAS/AF, the colon is used as a section label indicator in label- There is still a problem – the variable values have not been
return structures to group code for frame labels or other frame separated correctly. If an ampersand (&) is also added to the
entries. The following sections define the code for a ‘RUN’ and a input statement:
‘PRINT’ button in a frame: input student : & $14. city & $30.;
RUN:
if levelte='_Required_' then do; The output becomes what we wanted:
_msg_='You must select a level before OBS STUDENT CITY
running the report!!'; 1 Jim Smith Washington DC; L.A.
return; 2 Key Jones Chicago
end;
_msg_='Please wait while your request is In the above example, the colon causes the input statement to
processed.'; read from the next non-blank character until the pointer reaches
refresh; the next blank column. The ampersands(&) tell SAS that single
call display('[Link]',name,levelte); blanks may be embedded within character values. The
return; combination of colon and ampersand causes the input statement
to read from the next non-blank character until the pointer
PRINT: reaches double blanks. Also note that, due to the semi-colon
rc=woutput('print', within the data, a CARDS4 statement and 4 semi-colons (;;;;) are
used to indicate the beginning and the end of the data lines.
'[Link]');
rc=woutput('clear');
For output, a colon preceding a format in a PUT statement forms
return;
a ‘Modified List Output’. Modified List Output generates different
results compared to that from a Formatted Output. List output and
formatted output use different methods to determine how far to
2. FORMAT MODIFIER move the pointer after a variable value is written. Modified list
The colon as an input/output modifier is documented in SAS output writes the value, inserts a blank space, and moves the
manuals. You can find examples of its use if you know to search pointer to the next column. All leading and trailing blanks are

2
Coders' Corner

deleted, and each value is followed by a single blank. Formatted FROM a dataset or table;
output moves the pointer the length of the format, even if the
value does not fill that length. The pointer moves to the next In the following example, we have a list of social security numbers
column; an intervening blank is not inserted. The following DATA (SSNs) to use as a basis for selecting matching records from a
step uses modified list output to write each output line: large, raw data file. There are a number of different ways to do
this. A macro variable created via SELECT INTO: is an efficient
data _null_; way.
input x y; *** get base SSN list;
put x : comma10.2 y : 7.2; data ssn_list;
datalines; input ssn $9.;
2353.20 7.10 datalines;
231 21 123456789
; 234567890
These lines are written to the SAS log, with the values separated 345678901
by an inserted blank: 456789012
----+----1----+----2 ;
2,353.20 7.10 run;
231.00 21.00 *** create a macro variable ssnok;
proc sql noprint;
In comparison, the following example uses formatted output: select ssn into :ssnok
put x comma10.2 y 7.2; separated by ', '
from ssn_list;
These lines are written to the SAS log, with the values aligned in quit;
columns: *** display the value of the macro variable
----+----1----+----2 in the LOG as
2,353.20 7.10 123456789, 234567890, 345678901, 456789012;
231.00 121.00 %put &ssnok;
*** dataset selected will contain only the
records of those SSNs in the base list;
3. OPERATOR MODIFIER
data selected;
In SAS, character strings must be adjusted to the same length
before they can be compared. When SAS compares character input @1 ssn @;
strings without the colon modifier, it pads the shorter string with *** use the macro variable &ssnok (a list of
blanks to the length of the longer string before making the SSNs) to filter records;
comparison. When SAS compares character strings with the if ssn not in (&ssnok) then return;
colon modifier after the operator, it truncates the longer string to input <list of variables>;
the length of the shorter string. This feature of the colon modifier output;
makes the comparison of a character string's prefix possible. For run;
example,
if zip='010' then do; Thanks to the macro variable &ssnok, the dataset ‘selected’ will
* This will pick up any zip which equals have only those observations whose social security numbers
'010' exactly; match the SSNs in dataset ‘ssn_list’. Note that there is a length
if zip=:'010' then do; limitation in this SELECT INTO: structure in case you are going to
* will pick up any zip starting with '010', generate a very long list. The length of the generated macro
such as '01025','0103', '01098'; variable cannot exceed 32K characters.
if zip>=:'010' then do;
* will pick up any zip from '010' up 5. VARIABLE NAME WILDCARD
alphabetically, such as '012', '21088';
A colon following a variable name prefix selects any variable
where lastname gt: ‘Sm’; whose name starts with that prefix. This useful feature can make
* will pick up any last name alphabetically it possible for a short statement to process a large group of
higher than ‘Sm’, such as ‘Smith’,‘SNASH’, variables whose names begin with the same prefix. For example,
‘Snash’; drop hi:;
*** This will drop any variable whose name
The colon modifier can follow all comparison operators (=:, >=:, starts with 'hi', such as high, hi, hifi;
<=:, ne:, gt:, lt:, in:) to conduct prefix comparison. The following data b; set a(keep=d:);
'in:' operation will select the students located in zip codes which *** This keeps every variables whose name
begin with '010', '011', '0131', '0138', respectively: starts with d, such as d1, d2, degree, date;
data s; set student; total=sum(of times:);
if zip in:('010','011','0131','0138'); *** The function sums all variables with the
prefix of ‘times’. Note this last form is
4. KEY WORD COMPONENT invalid under version 6.12 or lower for
In PROC SQL, the colon is part of the “SELECT… INTO :” Windows;
structure. This structure turns the values of a selected field into a
string of a macro variable for later use. The syntax of this 6. ARRAY BOUND DELIMITER
structure is: We can use a method called array processing to perform the
SELECT field-name1, . . ., field-nameN same tasks for a series of related variables within a dataset. The
INTO :macro-variable-name1,. . ., variables involved in the processing are related by arrays. An
:macro-variable-nameN, array is a temporary grouping of SAS variables that are arranged
[SEPARATED BY 'a blank, a character, or a in a particular order and identified by an array-name; its definition
character string' [NOTRIM]] is valid only in the same data step.

3
Coders' Corner

index creation operator to create a row vector. The syntax for the
By default in SAS, the subscript in each dimension of an array vector creation is:
ranges from 1 to n, where n is the number of elements in that rowname = value1:valueN;
dimension. In an array statement, the lower bound can be omitted
if its value is 1. An array with 2 dimensions ranging from 1 to 3 In the statement, the colon delimits the first element and the last
and 1 to 6, respectively, can be declared as: element of the index row vector. When the first element of the
array test{3,6} test1-test18; vector is smaller than the last element, the elements in the row
vector increment one by one from the left to the right. For
Using a formal Bounded Array Declaration, we can declare this example, the statement
same array as: normalrw = 4:9;
array test{1:3,1:6} test1-test18;
results in
In a bounded array declaration, the colon is used to separate the normalrw 1 row 6 cols (numeric)
lower and upper bounds of an array’s dimension. The bounded 4 5 6 7 8 9
array declaration is useful in defining array dimensions which are
not based on 1. In the following case, defining the array bounds
When the first element of the vector is larger than the last
according to year numbers makes the code more readable and
element, a reverse order index is created with an incremental
reusable:
value of –1. This statement
array sales{1980:2000} sales1980-sales2000;
reverserw = 10:3;
do i=lbound(sales) to hbound(sales);
. . .
results in
if sales{i}<0 then do;
reverserw 1 row 8 cols (numeric)
call symput('y',i);
10 9 8 7 6 5 4 3
call symput('sales',sales{i});
%put &y: Sales Error! Sales&y=&sales;
When the elements of the vector are character arguments with a
end;
numeric suffix, the arguments should be put in quotation marks.
. . .
The following statement
end; charrw = ‘month3’:’month7’;

Suppose that ‘sales1987’ has a sales value of –5,000, that is,


generates an index row of
sales{1987}=-5000. The above code will put this line in the log
charrw 1 row 5 cols (character)
(given that ‘options symbolgen;’ is in effect):
month3 month4 month5 month6 month7
1987: Sales Error! Sales1987=-5000;

If the element’s increment is not 1 or -1, a DO function should


7. ARGUMENT FEATURE DELIMITER replace the colon delimited phrase ‘value1:value2’ to generate the
In SAS/AF, a colon can be used both as an argument’s feature index row vector.
delimiter and as a label indicator. In a METHOD statement, each
argument type must be declared and colons are used to separate
the elements of declaration: CONCLUSION
label: method public In SAS language, a colon (:) can be used in many different ways.
arg1:output:num This paper explained how it may be used as a label indicator, a
format modifier, an operator modifier, a key word component, a
arg2:input:char(32)
variable name wildcard, an array bound delimiter, an argument
endmethod; feature delimiter, a special log indicator, and an index creation
operator. Mastering these different uses can give you added
In the argument segments of the above code, the first element, flexibility in writing efficient, highly functional code.
e.g. ‘arg1’, is the argument’s name, the second, e.g., ‘output’, is
the argument’s usage, and the third, e.g., ‘num’, is the argument’s
data type. REFERENCES
SAS Institute: SAS OnlineDoc, version 8, 1999
SAS Institute: SAS System Help, V6, V8, 1996, 1999
8. SPECIAL LOG INDICATOR Grant, Paul: SUGI 23: Simplifying Complex Character
In SAS for Windows version 8, a colon can be combined with Comparisons by Using the IN Operator and the Colon (:) Operator
keyword ERROR, NOTE, or WARNING in a %PUT statement to Modifier, 1998
generate customized error, note, or warning text in SAS log.
Cody, Ronald P.: SUGI 23: The INPUT Statement: Where It's @,
These particular user-defined log strings have the same color as
1998
the system generated error, note, and warning messages. In the
%PUT statement, the keyword must be the first word after %PUT, Kuligowski, Andrew T.; Roberts, Nancy: SUGI 23: Basic Methods
must be in upper case, and must be followed immediately by a to Introduce External Data into the SAS System, 1998
colon or a hyphen. The following statements Timbers, Vincent L.: SUGI 23: SAS/AF Frame Entries: A Hands-
%put ERROR: You made a wrong turn!; on Introduction, 1998
%put NOTE: Your Lucky Number IS - &lucky.; Olson, Diane: SUGI 25: Power Indexing: A Guide to Using
%put WARNING: Stop and Check!; Indexes Effectively In Nashville Releases, 2000
Jaffe, Jay A.: SUGI 24: SAS Macros: Beyond the Basics, 1999
Satchi, Thiru: SUGI 24: An SQL List Macro to Retrieve Data from
generate these colored strings in the log:
Large SAS/DB2 Databases, 1999
ERROR: You made a wrong turn! (Burgundy)
NOTE: Your Lucky Number IS - 9058. (Blue)
WARNING: Stop and Check! (Green) ACKNOWLEDGEMENT
I appreciate the contribution from Arthur L. Carpenter of California
Occidental Consultants and Phillip Friend of USDA as well as my
9. INDEX CREATION OPERATOR colleagues in the Department of Veterans Affairs.
In SAS/IML(Interactive Matrix Language), a colon can serve as an

4
Coders' Corner

CONTACT INFORMATION
Hope you can provide your cases of using colons in SAS. Please
contact the author at:
Author Name Haiping Luo
Email hpluo@[Link]
SAS Discussion Board [Link]
[Link]

SAS and all other SAS Institute Inc. product or service names are
registered trademarks or trademarks of SAS Institute Inc. in the
USA and other Countries.  indicates USA registration. Other
brand and product names are registered trademarks or
trademarks of their respective companies.

You might also like