BookmarkSubscribeRSS Feed
dkanand86
Calcite | Level 5

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

10 REPLIES 10
ballardw
Super User

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.                                                                                                                            


dkanand86
Calcite | Level 5

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

Is there any alternative way of doing this?

Thanks for this solution.

SASKiwi
PROC Star

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.

dkanand86
Calcite | Level 5

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

Astounding
PROC Star

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.

Ron_MacroMaven
Lapis Lazuli | Level 10

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;

Ron_MacroMaven
Lapis Lazuli | Level 10

%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;

dkanand86
Calcite | Level 5

Ok thanks

dkanand86
Calcite | Level 5

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;

Ron_MacroMaven
Lapis Lazuli | Level 10

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 7711 views
  • 7 likes
  • 5 in conversation