BookmarkSubscribeRSS Feed
mftuchman
Quartz | Level 8
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.
10 REPLIES 10
mftuchman
Quartz | Level 8
ps. I usually indent my code, but the browser removed all my spaces. Forgive me!
Cynthia_sas
SAS Super FREQ
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]
mftuchman
Quartz | Level 8
Thanks! I had forgotten that method, and I actually had used it in my code, now that I think of it.
mftuchman
Quartz | Level 8
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.
Cynthia_sas
SAS Super FREQ
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
mftuchman
Quartz | Level 8
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
Peter_C
Rhodochrosite | Level 12
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


.
data_null__
Jade | Level 19
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.
mftuchman
Quartz | Level 8
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
SAS_user
Calcite | Level 5
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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 2533 views
  • 0 likes
  • 5 in conversation