Date: Thu, 4 Mar 2010 08:44:37 -0500
Reply-To: Jim Groeneveld <jim.1stat@YAHOO.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Jim Groeneveld <jim.1stat@YAHOO.COM>
Subject: Re: check for variable existence, if not there put variable in.
Another solution is, without actually knowing whether a variable already
exists or not, is to define it with an initialised value of missing:
RETAIN CheckVar .; * this is the crucial statement! ;
The RETAIN may also be specified before the SET, it does not matter.
It initialises the variable to missing (.) for every record and that's it.
Once the dataset contains the variable (it is implicitly retained and) the
dataset's values overwrite the initialised (missing) ones.
This may also be done with character variables (RETAIN CheckVar '';), but
care should be taken to retain another type than is in the dataset already.
Regards - Jim.
Jim Groeneveld, Netherlands
Statistician, SAS consultant
On Wed, 3 Mar 2010 15:32:12 -0600, Catima Potter <cpotter@KUMC.EDU> wrote:
>I am writing code where I need to see if a variable exists in the
>dataset. If it is not there, then I would like to include it with a
>value of . (missing). I have tried this code but when the variable does
>exists, it is overwriting the values to missing. Your assistance is
>greatly appreciated. ~Catima
> input _20093 _20094 rate $;
>3 1 staffing
>5 2 staffing
>7 3 staffing
>9 4 staffing
>11 5 staffing
>13 6 staffing
>15 7 staffing
>17 8 staffing
>19 9 staffing
>%let pastyrqtr = _20093;
>if symexist('&pastyrqtr') then put '**** &pastyrqtr exists';
>else &pastyrqtr = .;