Skip to content

Fanuc macros: parametric programming with variables

Fanuc macros, known as macro B, let you write programs with variables, calculations, conditions and loops instead of fixed dimensions: rather than listing twenty-four holes, you write a loop that calculates them. A variable is a memory cell named with a pound sign and a number, like #100, and it can go anywhere a number would go: X#100 moves to the position held in that variable. It is the tool behind family-of-parts programs, the shop's own cycles, probing and safety checks.

For reference only: syntax changes with the control, the post-processor and the machine parameters. Check the official manual and run a simulation before cutting. Legal notice.

The brands and control names mentioned belong to their owners and are used only to identify the equipment. GetGCode is not affiliated with or endorsed by any manufacturer, ISO or ASME: this is our own content, not official documentation. Legal notice.

Family
Macros
Machine
Milling, Turning, Mill-turn
Risk
High
Example
#100=10 / IF[#100GT5]GOTO20 / WHILE[...]DO1

Syntax by control

Fanuc (assignment and calculation)
#100 = 25.0 / #101 = [#100 * 2] + 3.5
Square brackets are only needed for grouping, and you never group with parentheses: parentheses are comments. Arithmetic operators + - * / and the functions SIN, COS, TAN, ATAN, SQRT, ABS, ROUND, FIX, FUP.
Fanuc (condition and jump)
IF [#100 GT 50] GOTO 100 / IF [#100 EQ 0] THEN #101 = 1
Comparisons are written with letters: EQ equal, NE not equal, GT greater than, GE greater than or equal, LT less than, LE less than or equal. GOTO jumps to a block with an N number; THEN runs a single assignment without jumping.
Fanuc (loop)
WHILE [#100 LE 360] DO 1 / ... / END 1
Loops are numbered 1 to 3 and can be nested up to three levels, but every DO has to close with its END of the same number and loops cannot cross.
Fanuc (macro call)
G65 P9010 A50. B30. C12. / G66 P9010 ... / G67
G65 calls program O9010 once and passes the letters to it as arguments. G66 makes the call modal and G67 cancels it. Each letter arrives inside the macro as a specific local variable: the mapping table is in your control's macro manual.
Haas
#100 = 25. / IF [#100 GT 50] GOTO 100 / G65 P9010 A50.
Haas uses the same basic syntax as Fanuc, and on current controls macros come standard. System variables and some Haas-specific functions do not match Fanuc's: check the variable list in the operator's manual.
Siemens 840D / 828D
DEF REAL PROFUNDIDAD = 25.0 / IF ... ENDIF / FOR ... ENDFOR
Siemens does not use macro B: it has its own language with variables declared by name and type, and its own control structures. There is no direct translation of a Fanuc macro.

Address letters used with it

LetterWhat it setsUnit
#1 to #33Local variables: the ones that receive the arguments of a G65 call and the ones used as helpers inside the macro. They are cleared when the macro ends and each nesting level has its own.real number or empty
#100 to #199Volatile common variables: every program and macro can see them, but they are cleared when the machine is powered off. These are the ones for calculations that only matter during the program.real number or empty
#500 to #999Persistent common variables: they survive power-off. These hold shop data, such as the measured wear of a tool or a part counter. The exact range available depends on the control model and the installed option.real number or empty
#1000 and upSystem variables: machine positions, active G codes, offsets, alarms, PLC signals. The specific numbers change between controls and between types of offset memory, so look them up in the manual of the installed control.depends on the variable
#0The empty variable. It is not zero: a dimension written with an empty variable simply does not exist in the block. Comparing with #0 is how you find out whether an argument was passed or not.no value
[ ]Expression brackets. They are only needed for grouping; parentheses are comments in a Fanuc program and do not group anything.no unit

Worked example

Parametric bolt circle: number of holes, circle diameter, center and start angle are written once at the top and the loop calculates the positions with sine and cosine. It is the classic example because it shows the four things in a macro: variables, a math expression, a WHILE loop and using a variable inside a dimension. Change the four numbers in the header and you get any other bolt circle.

O0195 (CIRCULO DE AGUJEROS PARAMETRICO)
( Program header. )
G21 G17 G40 G49 G80
( Millimeters, XY plane and a clean state. )
#100 = 8
( Number of holes. This is the first number you touch for a different part. )
#101 = 120.
( Bolt circle diameter. )
#102 = 60.
( X coordinate of the circle center. )
#103 = 50.
( Y coordinate of the center. )
#104 = 0.
( Angle of the first hole, measured from the positive X axis. )
#105 = 360. / #100
( Angular step between holes. It is calculated, not typed in: if tomorrow there are twelve holes, only #100 changes. )
#106 = 1
( Loop counter. )
G91 G28 Z0.
( Z to the reference point before the change. )
T7 M06
( Ø8 drill. )
G90 G54 G00 X#102 Y#103
( Positions over the center of the circle, using the variables directly as dimensions. )
G43 H07 Z50. M03 S2500
( T7 length offset and spindle on. )
M08
( Coolant. )
WHILE [#106 LE #100] DO 1
( Starts the loop: it repeats as long as the counter does not go past the number of holes. )
#107 = #102 + [[#101 / 2] * COS[#104]]
( X coordinate of the current hole: center plus radius times the cosine of the angle. Note the nested brackets; parentheses do not work here. )
#108 = #103 + [[#101 / 2] * SIN[#104]]
( Y coordinate of the current hole, with the sine. )
G99 G81 X#107 Y#108 Z-20. R3. F200
( Drills at the calculated position. The canned cycle accepts variables in X and Y as if they were numbers. )
#104 = #104 + #105
( Adds the angular step for the next hole. )
#106 = #106 + 1
( Increments the counter. Forgetting this line is the number one mistake: the loop never ends. )
END 1
( Closes loop 1. The number has to match the one on the DO. )
G80 G00 Z50. M09
( Cancels the cycle and moves up. )
G91 G28 Z0. M05
( Z to the reference point and spindle stop. )
G90 M30
( Back to absolute and end of program. )
Open the simulator to check the example

Practical keys to using Fanuc macros in the workshop

Which variable range to use for what

Picking a range is not cosmetic: it changes the behavior. Locals #1 to #33 are born and die inside each macro, so two nested macros do not step on each other's variables. Commons #100 to #199 are visible to everything and are cleared at power-off: perfect for calculations within a program. #500 and up survive power-off, and that is where shop data goes, like the result of a measurement or a part counter. The exact range available depends on the model and the installed macro option, so before you decide where to store something important, check what your control has.

G65, arguments and why locals exist

G65 P9010 A50. B30. calls program O9010 and passes it values. Inside the macro those values are not read as A and B but as specific local variables, and the table of which letter lands in which number is in the macro manual. That mechanism is what turns a macro into a shop cycle of your own: you write it once, store it protected and call it from any program with the part data. The mandatory check inside the macro is whether each argument arrived, by comparing it with #0: if someone calls it without the depth, a clear alarm beats a move to zero.

Where macros break on the real machine

Three places. First, loops with no exit: if the counter does not increment or the condition never stops being true, the machine sits there looping without moving anything and looks hung. Second, lookahead: the control reads blocks ahead, and a variable calculated in one block may not be written yet when another one uses it, which with probe measurements means you have to force synchronization. Third, decimals: without a decimal point, an address like Z25 is read in the smallest unit (0.025 mm), not as 25 mm.

Mistakes that cost you a part (or a tool)

  • Using parentheses instead of brackets in an expression: on Fanuc parentheses are comments, so #101 = (#100 * 2) is left with no calculation and gives a format alarm.
  • Forgetting to increment the counter inside a WHILE: the loop never ends and the machine looks hung, with no alarm and no motion.
  • Closing a DO 1 with an END 2 or crossing two loops: the control gives a structure alarm, and if it does not, the program does something other than what it reads.
  • Storing important data in the #100 to #199 range and finding out the next day that it was cleared when the machine was powered off.
  • Copying system variable numbers from a manual for a different model: offsets, positions and signals change numbers between controls and between types of offset memory.
  • Calling a macro without checking inside whether the arguments arrived: a missing argument arrives as an empty variable and the dimension simply is not written.
  • Writing a macro that modifies offsets or parameters without protecting it: anyone who runs it by mistake throws the machine off for all the programs that follow.

Frequently asked questions

What is macro B?

It is the name of Fanuc parametric programming: variables with a pound sign, math expressions, IF conditions, WHILE loops and calls with G65. The earlier version, macro A, used a much more rigid syntax; today practically everything you see is macro B.

What is the difference between #100 and #500?

Both are common variables visible to any program, but #100 to #199 are cleared at power-off and #500 and up are not. That is why data that has to survive, like a measurement or a counter, is stored from #500 up.

Why does my expression not calculate anything?

Almost certainly because it is written with parentheses instead of brackets. In a Fanuc program everything inside parentheses is a comment, so the calculation disappears: #101 = (#100 * 2) is left as #101 = and gives a format alarm.

Do macros work the same on Haas as on Fanuc?

The basic syntax of variables, conditions, loops and G65 is practically the same. What changes is the system variables and some brand-specific functions, so a macro that only calculates geometry usually carries over as is, and one that reads machine states does not.

Can I use macros to make my own cycles?

Yes, and that is their best use: you write the macro as a separate program, protect it so it cannot be edited by accident and call it with G65, passing it the part data. Some controls also let you tie it to a free G code, so it is used as if it were a factory cycle.

Related codes

Other codes in Macros

See the full list of G and M codes

Related apps