Error - : near "begin": syntax error, unexpected begin, expecting function or task

I create my own macros file:

`ifndef MY_MACROS_SV
`define MY_MACROS_SV

// MACRO: 'my_fatal_err
// calls uvm_fatal in case the assertion is not correct
`define my_fatal(condition, msg)\
   assert (condition) else\
`uvm_fatal("FATAL ERROR", msg)

`define add_rand(mem_type, mem) \
  begin \
   case (mem_type) \
     "int": add_rand_int(mem); \
     "bit": add_rand_bit(mem); \
     default: `uvm_fatal("FATAL ERROR", "type is not supported") \
    endcase\
  end

`endif  //MY_MACROS_SV

I got the following error:
while parsing macro expansion: ‘add_rand’ starting at …\sv\tx_transaction.sv(17)
at …\sv\tx_transaction.sv(17): near “begin”: syntax error, unexpected begin, expecting function or task.

Line 17 in tx_transaction.sv:

  `add_rand("int", mem_int)

In reply to saritr:

You have probably called this macro outside function/task.


class transaction;

// some variables
 function void any_thing();
  // do something
  `add_rand("int", mem_int)
 endfunction : any_thing

 // `add_rand("int", mem_int) // you should not call it outside task or function as it is procedural block.

endclass : transaction

Call this macro inside function/task