Example: Creating a Named Varlist
Here is an example that
creates a named varlist. The varlist is passed as an argument to
the variable information functions VNAME and VTYPE to get name and type metadata
about the variables in the varlist.
proc ds2;
data _null_;
vararray double x[5];
dcl bigint xyz;
dcl date xanadu;
varlist t1 [x:]; /* 1 */
method printNames(varlist v); /* 2 */
dcl varchar(100) name type;
dcl bigint i;
do i = 1 to dim(v); /* 3 */
name = vname(v[i]);
type = vtype(v[i]);
put name= type=;
end;
end;
method init();
printNames(t1); /* 4 */
end;
enddata;
run;
quit;
-
The VARLIST statement specifies to create varlist
t1and specifies to select from the available global variables using the name prefixx:. -
A printNames method is defined, which takes a varlist as input. The varlist is referred to as
vin the body of the method. The method also defines local variablesname,type, andi. -
Next in the method, the DO statement is used with the DIM function to iterate through the variables in varlist
v. For each variable, metadata is retrieved with the VNAME function and the VTYPE function and assigned to local variablesnameandtype. The PUT statement prints the values from variablesnameandtypeto the SAS log. -
Varlist t1 is passed to the printNames method as a method argument in the INIT() method.
Here is the result that is written to the SAS log:
name=x1 type=double name=x2 type=double name=x3 type=double name=x4 type=double name=x5 type=double name=xyz type=bigint name=xanadu type=date
For more information, see the VARLIST Statement in SAS DS2 Language Reference.