DATA Step, Macro, Functions and more

how to ignore a variable if it is not present in a statement and continue the program

Reply
Frequent Contributor
Posts: 116

how to ignore a variable if it is not present in a statement and continue the program

i am wondering if we can ignore a variable in a statement if it doesnt exist and continue with the data step.

 

data mgh;

   set ae;

  if strip(aecon)="" and strip(acn)="" and strip(withd)="" then ae_use="";

 else if aecon="N" and strip(upcase(acn))

run;

 

in some datasets i do not have aecon variable and when i run with them it gives error. can i do something which makes it run without error if the aecon variable is missing and the rest of the program runs as usual.

 

any suggestions

Super User
Posts: 5,096

Re: how to ignore a variable if it is not present in a statement and continue the program

Your easiest fix would be to make sure both variables exist.  For example:

 

data mgh;

length acn aecon $ 8;

set ae;

if ........;

run;

 

The LENGTH statement will add variables that don't currently exist.  You are guaranteed to have both variables in your data set at the conclusion of the DATA step (even if one of them always contains a blank value).

 

There are more complex approaches that use macro language to determine which variables exist.  However, macro language isn't appropriate until you have learned more SAS.

Frequent Contributor
Posts: 116

Re: how to ignore a variable if it is not present in a statement and continue the program

Thanks for the solution.

 

In fact i do not have that variable in my dataset but was making some sort of standard program where it can be used for other studies and they have that variable.

So was thinking how to make it run

Super User
Super User
Posts: 7,430

Re: how to ignore a variable if it is not present in a statement and continue the program

If you were making a standard program to run across many studies, then you would of course have followed Software Development Lifecycle techniques.  You would assess what inputs/outputs the code would have, document all the functional requirements of the code, developing testing plan and user documentation.  Once all that is done actually coding such a thing is pretty simple, as the documentaion will highlight all the required information.  For instance, if I was to asssess the process for the code you have provided and your requirements, I would come to the conclusion that the list of check values is not necessarily fixed, hence an input would be the list of variables to be checked, we can thus make this a parameter to the macro call;

%macro General_Macro (inds=,outds=,list_of_vars=);
  data &outds.;
    set &inds.;
    if cats(of &list_of_vars.)="" then ae_use="";
  run;
%mend General_Macro;

%General_Macro (inds=ae,outds=mg,list_of_vars=aecon acn withd);

As with any programming, defining the specifications and documentation is 99% of the task.

Super User
Posts: 5,096

Re: how to ignore a variable if it is not present in a statement and continue the program

To plan for that, here is the minimum amount of work that you would need to do.

 

For a data set that contains ACN but not AECON, spell out the complete set of IF/THEN statements that would be necessary to use in a DATA step.  Test and debug the code.

 

For a data set that contains AECON but not ACN, same thing.

 

For a data set that contains both ACN and AECON, same thing.

 

Once you have working code that handles all the possible situations, we can consider the question of getting SAS to use the right set of code.  But that code has to exist (100% debugged and working) first.

Ask a Question
Discussion stats
  • 4 replies
  • 198 views
  • 6 likes
  • 3 in conversation