DATA Step, Macro, Functions and more

Finding numeric Variables

Reply
Contributor
Posts: 42

Finding numeric Variables

Let's say you want a list of all the numeric variables in a data set. There are several ways you could do it, including running

* a proc contents with an out option, then filter out the type=1 (numeric) variables
* proc means with no VAR statement and an OUTPUT statement.
* a data step with obs=1 and iterating over an array

data _null_;
set sashelp.class(obs=1);
array s{*} _numeric_;
do i = 1 to dim(s);
n = vname(s{i});
put n;
end;
run;

Are there others? What is your favorite method? The proc means method is unsuitable for very large data sets, but does give the sample means as additional info.
Contributor
Posts: 42

Re: Finding numeric Variables

ps. I usually indent my code, but the browser removed all my spaces. Forgive me!
SAS Super FREQ
Posts: 8,743

Re: Finding numeric Variables

Hi:
I'd use SASHELP or DICTIONARY tables. See below.

Also, for future reference, refer to this posting for how to use [pre] and [/pre] to preserve indentation:
http://support.sas.com/forums/thread.jspa?messageID=27609毙

cynthia

[pre]

** 1) Create table, but could just print from SQL;
proc sql;
create table structure as
select libname, memname, name, type, length,
format, informat, label
from dictionary.columns
where libname = 'SASHELP' and
memname = 'SHOES' and
upcase(type) = 'NUM';
run;
quit;

ods listing;
proc print data=work.structure;
title '1) Use DICTIONARY.COLUMNS';
run;

** 2) Just print directly from SASHELP.VCOLUMN;
PROC PRINT data=sashelp.vcolumn;
title '2) Use SASHELP.VCOLUMN';
where libname = 'SASHELP' and
memname = 'SHOES' and
upcase(type) = 'NUM';
var libname memname name type length format informat label;
run;
[/pre]
Contributor
Posts: 42

Re: Finding numeric Variables

Thanks! I had forgotten that method, and I actually had used it in my code, now that I think of it.
Contributor
Posts: 42

Re: Finding numeric Variables

So, now we have four methods

* a proc contents with an out option, then filter out the type=1 (numeric) variables
* proc means with no VAR statement and an OUTPUT statement.
* a data step with obs=1 and iterating over an array
* Dictionary (and equivalently SASHELP) tables.
SAS Super FREQ
Posts: 8,743

Re: Finding numeric Variables

I'm sure that all of those methods -would- work, But I'm not sure I'd use or recommend anything but the DICTIONARY or SASHELP method. OK, maybe the PROC CONTENTS method.

But I would never use the PROC MEANS or the DATA step methods -- for one thing, I usually teach beginners and using PROC MEANS without a VAR statement -could- get them into bad habits of MEANS usage, when the other two methods work quite well. And for my students, ARRAY usage is generally beyond their level -and- unnecesary, when the DICTIONARY.TABLES or SASHELP or PROC CONTENTS methods work so well, and so simply.

cynthia
Contributor
Posts: 42

Re: Finding numeric Variables

In our server environment, dictionary tables take a disproportionately long time to populate since the each query on those views take a while to search through the entire environment.

On my local machine, dictionary tables are my favorite, followed by the CONTENTS procedure. I see your point about arrays being superfluous when the dictionary tables are available. Message was edited by: mftuchman
Valued Guide
Posts: 2,175

Re: Finding numeric Variables

sashelp.vcolumn should be no slower than dictionary.columns IF you use it in the SQL procedure.
I offer 2 more ways to work with variable or column names as data:
proc transpose data= sashelp.class( obs= 1);
proc print; run;
and, well demonstrated by dat _null_ (the poster not the code)
call vnext()
Probably all techniques have their place. We just have to understand when one is more suitable than the others.

another $0.02 from
PeterC


.
Respected Advisor
Posts: 3,777

Re: Finding numeric Variables

As Peter mentioned PROC TRANSPOSE is another canidate although I usually run it with OBS=0 so I don't create any COLn variables.

[pre]
proc transpose data=sashelp.shoes(obs=0 /*drop=_numeric_*/) out=numlist;
*var _numeric_; *or no var statement;
run;
[/pre]

I use similar PROC TRANSPOSE's to "expand" all sorts variable list macro variables, when writing dynamic programs. The advantage is that I can expand the list of words that may also contain a "SAS Variable List" such as A1-A10, AB: or double dashed lists, etc.

Plus if a variable does not exist then I get an nice error and message and I can examine the value of &SYSRC, I think that's the one, and stop the program or take other appropiate action.

As for PROC MEANS adding the data set opion OBS=1 would fix the data size problem, but I prefer PROC TRANPOSE.
Contributor
Posts: 42

Re: Finding numeric Variables

I found this very impressive. Useful tip! I think this is a favorite so far, particularly because we can use OBS=0 to pull the result.

The reason I originally use PROC MEANS is that I have to eventually report the means anyway. But I think this wins a prize for elegance. Message was edited by: mftuchman
Contributor
Posts: 48

Re: Finding numeric Variables

data _null_;
dsid = open("sashelp.class");
if dsid = 0 then stop;
nvars = attrn(dsid,"nvars");
do i = 1 to nvars;
if vartype(dsid,i) = "N" then
do;
varname = varname(dsid,i);
put varname =;
end;
end;
rc = close(dsid);
run;
Ask a Question
Discussion stats
  • 10 replies
  • 241 views
  • 0 likes
  • 5 in conversation