1
votes

I am trying to write a macro that creates a text file using parameter value. I understand all SAS parameters are passed as text, so i need to convert the text to numeric. Using INPUT for this but still getting a syntax error. Appreciate the help. Thank you.

code:

%macro test(n_var);

  data _null_;

    file"c:/temp/test.txt" TERMSTR=crlf;

    put ;
    put "(numeric variable passed = "input(&n_var,8.)")";
    put ;

  run;

%mend;

%test(n_var=100);

Log:

SYMBOLGEN:  Macro variable N_VAR resolves to 100
NOTE: Line generated by the macro variable "N_VAR".
39         100
           ___
           22
           76
MPRINT(TEST):   put "(numeric variable passed = "input(100,8.)")";
MPRINT(TEST):   put ;
MPRINT(TEST):   run;

ERROR 22-322: Syntax error, expecting one of the following: a name, arrayname, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.  

ERROR 76-322: Syntax error, statement will be ignored.
1
I don't understand. If you are writing to a text file then you are just writing text. What difference does it make whether the values passed to the macro look like numbers or words?Tom

1 Answers

2
votes

All SAS macro symbols (aka variables) are text. SAS macro parameters are text.

Your use case probably does not need to convert text to numeric.

Consider:

%let x = 100;
data _null_;
  my_num_var = &x;
run;

The resolution of the macro variable (or perhaps better understood as 'symbol') are the letters 1 0 0, but with respect to text to be interpreted as SAS code. The data step compiler infers my_num_var is numeric from the line it sees as

  my_num_var = 100;

There are some use cases where you may want to test that a macro parameter can be interpreted as a numeric value. Such use cases are probably beyond your needs at this time.

The INPUT function is one of those special DATA Step functions that is not available for use in SAS macro via the %sysfunc function. When you must 'input' a value in a 'pure' manner outside a DATA step, you will want to invoke the INPUTN or INPUTC functions via %sysfunc

%let evaluatedRepresentation = %sysfunc(inputn(&x,best8.));

The numeric evaluation of the inputn is converted to text and assigned to symbol evaluatedRepresentation.

If you are not in control of the callers to you macros in which you do ampersand evaluations the safer approach is to evaluate via SUPERQ to break code injections and other anamolies

%let evaluatedRepresentation = %sysfunc(inputn(%superq(x),best8.));