From: Joe Matise on 3 Mar 2010 16:36 You can always put if missing(var) then call missing(var); which is sort of cheating but works fine iff var is numeric... Or just give the variable a format and/or length - that will initialize the variable if it's not in existence at that point. -Joe On Wed, Mar 3, 2010 at 3:32 PM, Catima Potter <cpotter(a)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; >
From: "Data _null_;" on 3 Mar 2010 16:43 You could do it this way. Variable will default to Numeric. If it exists the type does not matter. 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 ; run; %let pastyrqtr = _20091; data need; if 0 then set have; retain &pastyrqtr; call missing(of _all_); set have; run; proc print; run; On 3/3/10, Catima Potter <cpotter(a)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; >
From: Jim Groeneveld on 4 Mar 2010 04:21 Hi Catima, SYMEXIST checks the existence of macro variables, not the existence of SAS variables in a data step (SET dataset). Instead you might want to use the function VARNUM. Based on SAS vs. 6 I already wrote a macro %ChkVar that uses the output of PROC CONTENTS for a SAS dataset in checking the existence of variables: http://jim.groeneveld.eu.tf/software/SASmacro/ChkVar.zip I may have to rewrite/simplify/disregard that macro by now. 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(a)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;
From: Jim Groeneveld on 4 Mar 2010 08:44 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(a)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;
|
Pages: 1 Prev: check for variable existence, if not there put variable in. Next: Can ODS create Excel output? |