BookmarkSubscribeRSS Feed
David_Billa
Rhodochrosite | Level 12

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.

10 REPLIES 10
David_Billa
Rhodochrosite | Level 12
Thanks, any other ways?
Reeza
Super User

You can loop through the variables using CALL VNEXT().

 


@David_Billa wrote:
Thanks, any other ways?

 

David_Billa
Rhodochrosite | Level 12
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?
Tom
Super User Tom
Super User

@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
Rhodochrosite | Level 12
Yes, I want to test whethe the variable has a non missing value
Reeza
Super User
Datasets typically have multiple records. When you say non missing value, is that for a specific row or all rows? If you have 10 records, 6 missing and 4 non missing what happens?
David_Billa
Rhodochrosite | Level 12
Would like to check for non missing values for all the rows
Tom
Super User Tom
Super User

@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.

 

Reeza
Super User
Probably overkill for what you need but here's some code that prepares a few variants of a missing report on your data.

You can run the code directly from GitHub and see the results and the different summary results.

https://gist.github.com/statgeek/2de1faf1644dc8160fe721056202f111
How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 6301 views
  • 1 like
  • 4 in conversation