/* vnmtp.sas Daniel Brockman 20070505 get list of vars & types from dataset */ /* * %vnmtp(dsn) gets the names and types of the variables * in data set &dsn . * * global var gvarn will contain the names. * global var gvart will contain the types for the vars in gvarn. * global var gvarnn will contain the names of the numeric vars. * global var gvarnc will contain the names of the char vars. * * Example: * * %global gvarn gvart gvarnn gvarnc; * %vnmtp(mydataset); * %put List of Variables in mydataset: &gvarn ; * %put ... and their respective types: &gvart ; * %put ... numerics: &gvarnn ; * %put ... characters: &gvarnc ; * * Caveat: * Please note %vnmtp() opens and closes datasets. In most cases, * you should use %vnmtp() once per data set per session to avoid * excessive io, which might slow your program. * * Inspiration: * http://support.sas.com/ctx/samples/index.jsp?sid=597 */ /*---------------------------------------------*/ %macro vnmtp(dsn); %local dsid ct i name type rc ; /* init macro vars */ %let gvarn=; /* global list of names */ %let gvart=; /* global list of types */ %let gvarnn=; /* global list of numeric names */ %let gvarnc=; /* global list of character names */ /* Open the data set */ %let dsid=%sysfunc(open(&dsn)); /* check for dataset existence */ %put L38 dsid: &dsid dsn:&dsn ; %if ( &dsid eq 0 ) %then %do ; %put vnmtp: ERROR: dataset &dsn not found ; %end ; %else %do ; /* get number of variables (CT) in the data set */ %let ct=%sysfunc(attrn(&dsid,nvars)); /* write lists */ %do i = 1 %to &ct; %let name=%sysfunc(varname(&dsid,&i)); %let type=%sysfunc(vartype(&dsid,&i)); %let gvarn=&gvarn &name; %let gvart=&gvart &type; %if ( &type = C ) %then %let gvarnc=&gvarnc &name; %if ( &type = N ) %then %let gvarnn=&gvarnn &name; %end; /* Close the data set */ %let rc=%sysfunc(close(&dsid)); %end ; /* if dsid eq 0 */ %mend vnmtp; /*---------------------------------------------*/