Could you please help me write a condition to check for one variable in a dataset using data step? I don't want to use SASHELP.VCOLUMN. Is there any function to tackle to check for variable?
I just need only the condition to check if variable exists in a dataset.
See @Toms solution in the thread
@David_Billa wrote:
How to check for the data if the variable exists?
I would like to know whether the variable exists and if exists whether
there is a data available or not?
That is a totally different question. Do you want to test if the variable EVER has a non-missing value?
Also what do you mean by test in a data step? Once you reference a variable in the code of a datastep the compiler will see the reference and create the variable for you.
Here is a data step that will test if a variable ever has any missing values and generate a macro variable you can use to drive some future decisions. This will test the dataset HAVE for the variable X and create the macro variable X_EXISTS with either 0 or 1 as the value.
data _null_;
if _n_=1 then call symputx('x_exists','0');
set have ;
if not missing(X) then do;
call symputx('x_exists','1');
stop;
end;
run;
@David_Billa wrote:
Would like to check for non missing values for all the rows
Please create examples and what you want.
For example here is code that checks every value of X in dataset HAVE and sets to dataset variables ANY_MISSING and ANY_POPULATED. It just writes them to the log , but you could make macro variables if you want.
data _null_;
retain any_missing 0 any_populated 0;
if eof then put any_missing= any_populated=;
set have end=eof;
if missing(X) then any_missing=1;
else any_populated=1;
run;
So for 2 binary variables there are 4 possible combinations.
any_missing=0 and any_populated=0 - means the dataset has NO observations.
any_missing=1 and any_populated=0 - means ALL of the observations have missing values for X.
any_missing=0 and any_populated=1 - means ALL of the observations have non-missing values for X, that is NONE of the observations have missing values of X.
any_missing=1 and any_populated=1 - means some observations have missing values for X and some have non-missing values of X.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.