Date: Tue, 29 Apr 2008 05:13:37 -0700
Reply-To: Peter <crawfordsoftware@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Peter <crawfordsoftware@GMAIL.COM>
Organization: http://groups.google.com
Subject: Re: Check if a column exists in a dataset?
Content-Type: text/plain; charset=ISO-8859-1
On Apr 29, 11:38 am, RolandRB <rolandbe...@hotmail.com> wrote:
> On Apr 29, 11:30 am, Eversmann <rifazraz...@gmail.com> wrote:
>
> > Hi,
>
> > I am designing a generic macro --- to do some processing on a given
> > dataset.
>
> > And a perticular column (say X) doesn't always exist.
>
> > is there a way to check if this column exist and do the processing
> > accordingly...
>
> > Many thanks in advance
>
> There are various ways of doing this. One of the simplest is using the
> sashelp.vcolumn view.
I presume you need a macro that returns tru/false (1/0) without going
through the statement boundary (let alone the step boundary)
open()
returns dsID
varnum()
returns 0 if varname is not in dsID
Culling from the manual and placing in a macro that returns a
variable's VARNUM
To demonstrate useage, here is a log from which you can parse the
macro.
Notice that the variable name search performed by the varnum()
function is not case-sensitive
3 %macro myVarN( var, data= &syslast )
4 / des= 'return VARNUM for a varname in
&data';
5 %local dsid rc vnum;
6 %let vnum = 0;
7 %let dsid=%sysfunc(open(&data,is));
8 %if &dsid > 0 %then %do;
9 %let Vnum=%sysfunc(varnum(&dsid,&var));
10 %let rc = %sysfunc(close(&dsid));
11 %end;
12 &Vnum
13 %mend myVarN ;
15 %put >>%myVarN( aGe, data= sashelp.class )<< ;
>>3<<
16 option _last_ = sashelp.class ; * and using default
"latest" dataset;
17 %put >>%myVarN( aGe)<< >>%myVarN( seX )<< ;
>>3<< >>2<<
PeterC
|