12

I am working on SAS in UNIX env and I want to view only the column name of a dataset. I have tried proc contents and proc print but both of them list a lot of other irrevelant information that I do not want as it fills up my putty screen and the information ultimately is lost.

I also tried to get this thing frm the sas metadata but that is not working either. I tried :

  2? proc sql;
  select *
 from dictionary.tables
 where libname='test' and memname='sweden_elig_file_jul';
quit;
  5?
NOTE: No rows were selected.

  6?
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.27 seconds
      cpu time            0.11 seconds
siso
  • 135
  • 1
  • 1
  • 7

4 Answers4

28

You're using the wrong dictionary table to get column names...

proc sql ;
  select name 
  from dictionary.columns
  where memname = 'MYDATA'
  ;
quit ;

Or using PROC CONTENTS

proc contents data=mydata out=meta (keep=NAME) ; 
run ; 
proc print data=meta ; run ;
Chris J
  • 7,549
  • 2
  • 25
  • 25
  • 6
    I second Chris J's answer. But, would not recommend the `dictionary.columns` way - it can be many times slower than the `proc contents` way especially if you have a large number of tables in your SAS environment. –  Jul 26 '13 at 10:07
  • The sql query approach didn't output the variable names for me, but the proc contents code worked fine. – RobertF May 23 '23 at 02:21
3

Here's one I've used before to get a list of columns with a little bit more information, you can add the keep option as in the previous answer. This just demonstrates how to create a connection to the metadata server, in case that is useful to anyone viewing this post.

libname fetchlib meta 
    library="libraryName" metaserver="metaDataServerAddress"
    password="yourPassword" port=1234
    repname="yourRepositoryName" user="yourUserName";

proc contents data=fetchlib.YouDataSetName
    memtype=DATA 
    out=outputDataSet 
    nodetails 
    noprint; 
run;
Scampbell
  • 1,535
  • 14
  • 24
0

For a pure macro approach, try the following:

%macro mf_getvarlist(libds
      ,dlm=%str( )
)/*/STORE SOURCE*/;
  /* declare local vars */
  %local outvar dsid nvars x rc dlm;
  /* open dataset in macro */
  %let dsid=%sysfunc(open(&libds));

  %if &dsid %then %do;
    %let nvars=%sysfunc(attrn(&dsid,NVARS));
    %if &nvars>0 %then %do;
      /* add first dataset variable to global macro variable */
      %let outvar=%sysfunc(varname(&dsid,1));
      /* add remaining variables with supplied delimeter */
      %do x=2 %to &nvars;
        %let outvar=&outvar.&dlm%sysfunc(varname(&dsid,&x));
      %end;
    %End;
    %let rc=%sysfunc(close(&dsid));
  %end;
  %else %do;
    %put unable to open &libds (rc=&dsid);
    %let rc=%sysfunc(close(&dsid));
  %end;
  &outvar
%mend;

Usage:

%put List of Variables=%mf_getvarlist(sashelp.class);

Returns:

List of Variables=Name Sex Age Height Weight

source: https://github.com/sasjs/core/blob/main/base/mf_getvarlist.sas

Allan Bowe
  • 12,306
  • 19
  • 75
  • 124
-1
proc sql;
    select *
    from dictionary.tables
    where libname="TEST" and memname="SWEDEN_ELIG_FILE_JUL";
quit;
Denis Bubnov
  • 2,619
  • 5
  • 30
  • 54
Sri Ram
  • 1
  • 1