Chapter Contents |
Previous |
Next |
LOCATEC and LOCATEN |
Category: | SAS Table |
Syntax | |
Details | |
Examples | |
Example 1: Using the LOCATEC Function | |
Example 2: Using the LOCATEN Function | |
See Also |
Syntax |
rc=LOCATEC(table-id,col-num,cval<,sort<,direction>>); |
rc=LOCATEN(table-id, col-num,nval <,sort<,direction>>); |
>0 | the number of rows read before a match is found |
0 | no row with a matching value was found |
Type: Numeric
Type: Numeric
Type: Numeric
Type: Character
Type: Numeric
'A' |
The table is sorted in ascending order. |
'D' |
The table is sorted in descending order. |
'U' |
The table is not sorted. (This is the default.) |
Type: Character
'A' |
searches all rows, starting with the first row. (This is the default.) |
'B' |
searches from the previous row backward. |
'F' |
searches from the next row forward. |
Type: Character
Details |
LOCATEC and LOCATEN do not search for partial values. For LOCATEC, preceding blanks are part of cval but trailing blanks are not. Therefore, you can facilitate searching for LOCATEC by using the LEFT function to left-justify character values.
LOCATEC and LOCATEN search all rows, starting with the first row by default and skipping rows marked for deletion. When a WHERE clause is active, these functions search the rows that meet the WHERE condition for a match. If a matching row is found, it is loaded into the Table Data Vector (TDV). Otherwise, the current row remains in the TDV.
LOCATEC and LOCATEN return the number of rows read before a match is found. This number may not correspond to the row number where the match is found because these functions skip deleted rows. Moreover, if a WHERE clause is active, they read only the rows, including any appended rows, that meet the WHERE condition. Also, if direction is supplied, the number returned is the number of rows read from the previous row where the search began. By default, the search direction is forward, starting with the first row in the table.
If the table is sorted, then specifying 'A'
or 'D'
for
sort uses the more efficient binary search algorithm. Perform
a binary search only when you have member-level access so that no one else
can be editing the table concurrently. With a binary search, LOCATEC and
LOCATEN make assumptions about how the data is sorted, and they assume that
they can identify the first and last rows. If the table is being edited concurrently,
rows could be appended so that the table is no longer in sorted order. As
a result, the binary search might not find the correct values.
Examples |
Locate a customer named SMITH in the PAYROLL table. The table is opened with a table-id of TABLEID and is sorted by NAME. The customer's name is specified in the CUSTOMER column.
customer='SMITH'; rc=locatec(tableid,varnum(tableid,'name'),customer,'a'); if (rc=0) then _msg_= 'There is no customer named '||customer||'.'; else do; ...more SCL statements... end; return;
Locate a house whose price is $94,000 in the SASUSER.HOUSES table, which is opened with a table-id of HOUSEID. The price is specified in the window variable PRICE.
houseid=open('sasuser.houses'); price=94000; rc=locaten(houseid,varnum(houseid,'price'),price); if (rc=0) then _msg_='No house is priced at '|| putn(price,'dollar9.2')||'.'; else do; rows=curobs(houseid); _msg_= 'The specified price was found in row '||rows; end; return;
See Also |
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.