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;
  1. The VARLIST statement specifies to create varlist t1 and specifies to select from the available global variables using the name prefix x:.

  2. A printNames method is defined, which takes a varlist as input. The varlist is referred to as v in the body of the method. The method also defines local variables name, type, and i.

  3. 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 variables name and type. The PUT statement prints the values from variables name and type to the SAS log.

  4. 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.

Last updated: August 17, 2026