Help using Base SAS procedures

Subset Proc contents results!

Reply
Contributor
Posts: 52

Subset Proc contents results!

Hi All,

I want to subset the proc contents results for a specific variable. How can i do that?

Note:

Keep in mind that the dataset have 10000 variables, and so i do not want proc contents of all 10000 variables, instead juist require contents info for specific variable.

thanks

Grand Advisor
Posts: 10,075

Re: Subset Proc contents results!

You can get characteristics of specific variables from dictionary.columns. This example pulls information from one variable in the SASHELP.Class data set.

proc sql;                                                                                                                              

   select *                                                                                                                            

   from dictionary.columns                                                                                                             

   where libname='SASHELP' and memname='CLASS' and upcase(name) = 'SEX';                                                                 

quit;     

The libname and memname(dataset) must be in all caps. I upcase the variable name for consistency. If you have a list of variable names of interest then use upcase(Name) in ('VARNAME1', 'VARNAME2',....,'VARNAME3') where VARNAMEs are the ones you are interested in.                                                                                                                            


Contributor
Posts: 52

Re: Subset Proc contents results!

Sometimes, when i run dictionary.columns on large dataset, the query hangs.

Is there any alternative way of doing this?

Thanks for this solution.

Respected Advisor
Posts: 3,001

Re: Subset Proc contents results!

PROC CONTENTS usually performs better for single tables.

Use it to get info on all of the columns for your table without printing but only to store in a SAS dataset.

Then use PROC PRINT or similar with a WHERE statement to display just the variables you want.

Contributor
Posts: 52

Re: Subset Proc contents results!

Please can you share the code for this, as i tried this, but didnt work as expected.

Respected Advisor
Posts: 4,787

Re: Subset Proc contents results!

Here's some of the code to get you started:

proc contents data=have noprint out=_contents_;

run;

proc print data=_contents_ (obs=10);

run;

This will give you an idea of what the output data set from PROC CONTENTS looks like, and possibly enough information for you to solve your original problem.

Regular Contributor
Posts: 187

Re: Subset Proc contents results!

you didn't say what you want to do with your output

here is code to get you started.

Check the OnLineDoc for the varnum function 

getting varnum is the key value you need in order to get format and label

in this program varnum is the loop index i

%let libname = sashelp;

%let memname = class;

DATA _null_;

dsid   = open ("&libname..&memname"      );

lrecl  = attrn(dsid,'lrecl');

n_obs  = attrn(dsid,'nobs ');

n_vars = attrn(dsid,'nvars');

do i = 1 to n_vars;

   name   = varname(dsid,i);

   type   = vartype(dsid,i);

   putlog name= type=;

   end;

rc = close (dsid);

stop;

run;

Regular Contributor
Posts: 187

Re: Subset Proc contents results!

%let libname = sashelp;

%let memname = class;

%let name = age;

proc contents data  = &libname..&memname

     out   = out_contents

     where = (upcase(name)

        eq "%upcase(&name)"));

proc print data = &syslast;

run;

Contributor
Posts: 52

Re: Subset Proc contents results!

Ok thanks

Contributor
Posts: 52

Re: Subset Proc contents results!

This code gives following error:


   %let libname = sashelp;


   %let memname = class;


   %let name = age;



   proc contents data  = &libname..&memname


        out   = out_contents;


        where  (upcase(name)


           eq "%upcase(&name)"));



22


76


clause.


22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant,


.


76-322: Syntax error, statement will be ignored.


    run;

Regular Contributor
Posts: 187

Re: Subset Proc contents results!

@dkanand86

LOL

well, you didn't copy and paste my code above.

what I wrote was a where clause for the output data set

what you typed is a where statement with unmatched parentheses.

Ask a Question
Discussion stats
  • 10 replies
  • 574 views
  • 7 likes
  • 5 in conversation