05-17-2017 04:02 AM
i am wondering if we can ignore a variable in a statement if it doesnt exist and continue with the data step.
if strip(aecon)="" and strip(acn)="" and strip(withd)="" then ae_use="";
else if aecon="N" and strip(upcase(acn))
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.
05-17-2017 04:08 AM
Your easiest fix would be to make sure both variables exist. For example:
length acn aecon $ 8;
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.
05-17-2017 04:14 AM
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
05-17-2017 05:04 AM
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.
05-17-2017 06:05 AM
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.