Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read variable names in a SAS data set?

Tags:

variables

sas

Are there any statements\functions capable of get the name of variables? Preferrably putting them into a column of another data set, a text field or a macro variable.

E.g.

- Data set 1

Name age sex

    Jk   14   F
    FH   34   M
  • Expected data set

    Var_name_of_dataset1

    Name
    age
    sex
    

PS: I know a statement: select into, which does sth relevantly It can read the value of a column into a field with customized separetors, and therefore wish there are similar ways of reading column names into a field or a column.

Thanks

like image 417
mj023119 Avatar asked Mar 21 '11 08:03

mj023119


People also ask

How do you read a variable in SAS?

When you want to control which variables are read into the program data vector, use the data set options in the statement (such as a SET statement) that reads the SAS data set. The options are generally more efficient than using the statements.

How do I extract column names in SAS?

The first option to list the column names of a SAS data set is with the PROC CONTENTS statement. With the syntax below you store the column names in a new table. If you don't specify the keep options, then the output table will contain not only the column names but also its format, its length, its type, etc.


2 Answers

PROC CONTENTS would be the quickest way to get that information in a dataset. Column names can be found in the column NAME.

proc contents data=sashelp.class out=contents noprint;
run;
like image 161
Laurent de Walick Avatar answered Oct 15 '22 14:10

Laurent de Walick


You can also use a datastep and array functions, e.g.

data colnames ;
  set sashelp.class (obs=1) ;

  array n{*} _NUMERIC_ ;
  array c{*} _CHARACTER_ ;

  do i = 1 to dim(n) ;
    vname = vname(n{i}) ;
    output ;
  end ;
  do i = 1 to dim(c) ;
    vname = vname(c{i}) ;
    output ;
  end ;
run ;
like image 41
Chris J Avatar answered Oct 15 '22 14:10

Chris J