SCL Lists |
SCL lists can be declared using the LIST data type, which is a reference type that stores the identifier assigned when you create the list. You assign the LIST type with the DECLARE statement. For example:
declare list carlist;
After you declare a list, you actually create it with the MAKELIST or MAKENLIST function. You can then insert numbers, characters, other lists, or objects into the list with the INSERTN, INSERTC, INSERTL, or INSERTO function, respectively. You can also specify the default number of items in the initial list by supplying an argument to the MAKELIST function. For example, the following statement makes a list that contains n items:
carlist=makelist(n);
Each of the n items is initialized to a numeric missing value. Note that n can be any nonnegative SCL numeric expression that is computed at run time, or it can be a simple nonnegative numeric constant such as 12, if you want to create a list with a known initial number of items. No matter how you create the list, you are free to expand or shrink it to contain as many items as you need, from 0 to as many items as your computer has memory to hold. To determine the length of a list, use the LISTLEN function.
Note: It is recommended that you declare lists with the LIST keyword to avoid problems in calling overloaded methods. See Overloading and List, Object, and Numeric Types
Example: Creating an SCL List |
This section shows an SCL program that creates an SCL list, along with the output that the program produces. The program reads the columns in DICTIONARY.TABLES, a special read-only SQL view that stores information about all the SAS tables and SAS views that are allocated in the current SAS session. The columns in this view are the attributes (items of information) that are available for SAS tables. The example program stores the column values in an SCL list that is sorted in table order (the order of the columns in the ATTRIBUTES view), name order, and length order.
To create the view that lists the SAS tables, submit the following SQL procedure from the PROGRAM EDITOR window:
/* Create the PROC SQL view ATTRIBUTES */ /* which contains information about all */ /* the members of type DATA in the SAS */ /* data libraries that are associated */ /* with the SAS session. */ proc sql noprint; create view attributes as select * from dictionary.tables where memtype="DATA"; quit;
The SCL program creates and displays an SCL list whose values come from the view ATTRIBUTES.
/* Declare COLUMNS as a list */ declare list columns; INIT: /* Open the view ATTRIBUTES for reading */ attrs=open('attributes', 'I'); if (attrs > 0) then do; /* Make a list containing the same */ /* number of items as the number of */ /* columns in the view ATTRS. */ numcols=attrn(attrs, 'NVARS'); columns=makelist(numcols); do i=1 to numcols; /* Set item I in list COLUMNS to */ /* the length of the column. The */ /* SETITEMN call is similar to */ /* the array assignment: */ /* array{i} = colLen; */ colLen=varlen(attrs, i); rc=setitemn(columns, colLen, i); /* NAMEITEM gives item I the name */ /* of the Ith column */ colName=varname(attrs, i); itemName=nameitem(columns, i, colName); end; sysrc=close(attrs); /* Print the column names in their */ /* order in the SAS table. Sort by */ /* name and print. Then sort by */ /* length and print. */ rc=putlist(columns,'SAS Table Order:',0); columns=sortlist(columns, 'NAME ASCENDING'); rc=putlist(columns, 'Name Order:', 0); vars=sortlist(columns,'value'); rc=putlist(columns, 'Length Order:', 0); /* Cleanup: delete the list */ rc=dellist(columns); end; else _msg_=sysmsg(); return;
This program produces the following output:
SAS Table Order:(LIBNAME=8 MEMNAME=32 MEMTYPE=8 MEMLABEL=256 TYPEMEM=8 CRDATE=8 MODATE=8 NOBS=8 OBSLEN=8 NVAR=8 PROTECT=3 COMPRESS=8 REUSE=3 BUFSIZE=8 DELOBS=8 INDXTYPE=9 )[5] Name Order:(BUFSIZE=8 COMPRESS=8 CRDATE=8 DELOBS=8 INDXTYPE=9 LIBNAME=8 MEMLABEL=256 MEMNAME=32 MEMTYPE=8 MODATE=8 NOBS=8 NVAR=8 OBSLEN=8 PROTECT=3 REUSE=3 TYPEMEM=8 )[5] Length Order:(PROTECT=3 REUSE=3 BUFSIZE=8 COMPRESS=8 CRDATE=8 DELOBS=8 LIBNAME=8 MEMTYPE=8 MODATE=8 NOBS=8 NVAR=8 OBSLEN=8 TYPEMEM=8 INDXTYPE=9 MEMNAME=32 MEMLABEL=256 )[5]
Note: [5] is the list identifier that was assigned when this example was run and may be different each time the example is run.
Copyright © 2009 by SAS Institute Inc., Cary, NC, USA. All rights reserved.