Desktop productivity for business analysts and programmers

Skip macro if nothing is assigned to it and continue to run code

Occasional Contributor
Posts: 13

Skip macro if nothing is assigned to it and continue to run code

This is the code I created, and it will stop running if it reaches a line where a macro is unassigned.


Data New;
          Set Old;
          If Value_1 in (Value_1_Items.)
          And Value_2 in (Value_2_Items.)
          And Value_3 in (Value_3_Items.)
          Then Output;


If a variable is blank/unassigned (lets say Value_2_Items.) , then I want the code to skip this line and to continue running the other macros.


Any ideas on how I would go about fixing my code? Thanks!

Super User
Posts: 13,913

Re: Skip macro if nothing is assigned to it and continue to run code

[ Edited ]

You might want to provide a more concrete example of some actual data that would 1) do the skip and 2) not do the skip.


And since there isn't any macro code OR macro variables shown it would help to show where this step lies in relation to the remaining code and indicate where in the macro code the execution would continue.


You imply that any of the Value_1 etc items may be unassigned. What should happen if all of then are unassigned? What if the first is unassigned?


Here is a small macro that can be used to check if a macro variable does not have anything assigned that I cribbed from someone on this forum awhile ago:

%macro isBlank(param);
%mend isBlank;
/* edited to add missing &*/

It returns 1 for yes it is blank, 0 for no it is not blank. This could be used inside another macro as:



%if %isBlank(sysdate) %then %put SYSDATE is Blank.

Note that the NAME of the macrovariable is passed, not with &sysdate.

If the macrovariable does not exist then you get a warning that the macro ISBLANK did not resolve.



%if %isblank(Value_2_Items) %then %do;



%else %do;

And Value_2 in (&Value_2_Items.)



for your specific example.

You will need to address that the inital IF in you data step versus AND bits for the subsequent variables if &Value_1_Items is blank.

Occasional Contributor
Posts: 13

Re: Skip macro if nothing is assigned to it and continue to run code

[ Edited ]

Okay, here's an example that should help provide more information.


Let's say Value_1_Items. = "Happy", "Sad"

In this case, the code should output a database called "New" showing data consisting of all "Happy" and "Sad" values that exist in Column "Value_1" from the database "Old".


Value_1_Items (same applies for Value_2_Items etc.) can be = "one string" in length

or = "two", "strings" (as seen with "Happy", "Sad" above)

or = more (like a list, if you get what I'm trying to say).


If all are unassigned, then there would be no output as there is nothing to run or find from database "Old" under the columns Value_1, Value_2, or Value_3.

If the first is unassigned, the code should ignore Value_1 and continue with Value_2 and Value_3 and output data that they contain from database "Old".


Therefore, I don't want to run a check because I just want the code to continue to run smoothly/automatically regardless of if one or two items are missing an assigned variable(s).

Ask a Question
Discussion stats
  • 2 replies
  • 1 like
  • 2 in conversation