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.
Hi Catima,
Another solution is, without actually knowing whether a variable already
exists or not, is to define it with an initialised value of missing:
DATA Included;
SET InclExcl;
RETAIN CheckVar .; * this is the crucial statement! ;
RUN;
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
http://jim.groeneveld.eu.tf
On Wed, 3 Mar 2010 15:32:12 -0600, Catima Potter <cpotter@KUMC.EDU> wrote:
>Hello,
>
>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
>
>
>data have;
> input _20093 _20094 rate $;
> cards;
>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;
>data have;
>set have;
>if symexist('&pastyrqtr') then put '**** &pastyrqtr exists';
>else &pastyrqtr = .;
>run;
|