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;
|
|
0
|
|
|
|
Reply
|
cpotter (7)
|
3/3/2010 9:32:12 PM |
|
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@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;
>
|
|
0
|
|
|
|
Reply
|
snoopy369 (1752)
|
3/3/2010 9:36:47 PM
|
|
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@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;
>
|
|
0
|
|
|
|
Reply
|
iebupdte
|
3/3/2010 9:43:34 PM
|
|
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@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;
|
|
0
|
|
|
|
Reply
|
jim.1stat (180)
|
3/4/2010 9:21:58 AM
|
|
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;
|
|
0
|
|
|
|
Reply
|
jim.1stat (180)
|
3/4/2010 1:44:37 PM
|
|
|
4 Replies
322 Views
(page loaded in 0.217 seconds)
Similiar Articles: check for variable existence, if not there put variable in. - comp ...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 valu... Check if a variable is a number - comp.lang.tclcheck for variable existence, if not there put variable in. - comp ... Hello ... irrational ... character variables, missing values are spaces, so ... Check if a variable is ... how do I check for existance of env var in bash - comp.unix ...... make it longer, what do I do if I can't make it ... check for variable existence, if not there put ... named ... are used and from there where go the variables to check if and ... how to use variable value as variable in the same dataset - comp ...check for variable existence, if not there put variable in. - comp ... how to use variable ... In Stata 11 and later, you can use the Variables Manager to see variable labels ... What is the option in Macro to display ERROR message if macro ...check for variable existence, if not there put variable in. - comp ... Hi Catima, SYMEXIST checks the existence of macro variables, not the existence of SAS ... Option Mprint - Retrieve Sas Code - comp.soft-sys.sascheck for variable existence, if not there put variable in. - comp ... Hello, I am writing code where I need to see if a ... Based on SAS vs. 6 I already wrote a macro ... Multiple data sets based on a variable - comp.soft-sys.sas ...Merging 2 datasets by 2 variables - comp.soft-sys.sas Multiple data ... check for variable existence, if not there put variable in. - comp ... Multiple data sets based on a ... Macro not resoved - comp.soft-sys.sascheck for variable existence, if not there put variable in. - comp ..... if macro ..... put %sysfunc(IFC(%symexist(Y),%nrstr(&Y),NOT FOUND)); %put ... Macro variables ... between-and operator - comp.soft-sys.sascheck for variable existence, if not there put variable in. - comp ... between-and operator - comp.soft-sys.sas check for variable existence, if not there put variable in ... Where used macro? - comp.cad.solidworks... message if macro ... check for variable existence, if not there put variable in. - comp ... Hi Catima, SYMEXIST checks the existence of macro variables, not the existence ... check for variable existence, if not there put variable in. - comp ...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 valu... check for variable existence, if not there put variable in. - sassas, check for variable existence, if not there put variable in. comp.soft-sys.sas - The SAS statistics package. 7/25/2012 12:12:38 AM
|