Date: Wed, 17 Sep 1997 09:11:30 -0400
Reply-To: Christianna Williams <christianna.williams@YALE.EDU>
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: Christianna Williams <christianna.williams@YALE.EDU>
Subject: Re: Checking for a variable on a data set.
Content-Type: text/plain; charset="us-ascii"
At 04:13 PM 9/16/97 -0400, you wrote:
>Does anybody have a good way to check and see if
>a variable is on a data set? I'm talking about
>from a programming point of view. It's not
>feasible to use PROC CONTENTS and verify the
>variables by hand.
>
>
I see you have already received some more elegant (efficient) responses, but
I'll throw in my
$0.02 anyway. The code below uses PROC CONTENTS with an output data set,
followed by PROC FORMAT with the CNTLIN option to create a format in which
all variables on the data set map to "OK", so one can check if a given
variable name is on the data set in question. The code was used within a
larger program, so some modifications might be needed.
%let ds=MYDATA;
%let depvar=MYVAR;
* --- Use proc contents with out= option to get variable labels ;
* Also used to ensure that the variables specified are on the ds;
proc contents data=&ds noprint out=tempcont;
run;
*create a format that maps all variable names on ds to 'OK' for checking;
data allvars (rename=(name=start)) ;
set tempcont (keep = name) ;
fmtname = '$ckvarf' ;
label = '0K' ;
run;
proc format cntlin=allvars;
run;
%macro ckvar(varname,ds) ;
%*check that outcome and exposure variables are on the input ds;
data _null_ ;
vt=put(upcase("&varname"),$ckvarf.) ;
call symput('ckvar',left(vt)) ;
run;
%if &ckvar ne 0K %then %do;
%put;
%put ************************************************************** ;
%put ***** ERROR!! VARIABLE &varname is not on input ds &ds!!! **** ;
%put ***** PROCESSING WILL STOP **** ;
%put ************************************************************** ;
%put;
%let continue=NO;
%end;
%mend ckvar ;
%ckvar(&depvar,&ds);
Christianna.Williams@yale.edu
Christianna S. Williams
Yale University Program on Aging
129 York Street, Suite 1N
New Haven, CT 06511
voice: (203) 764-9827
fax: (203) 764-9831