BookmarkSubscribeRSS Feed
vraj1
Quartz | Level 8

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

4 REPLIES 4
Astounding
PROC Star

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.

vraj1
Quartz | Level 8

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2306 views
  • 6 likes
  • 3 in conversation