I received the following email:
Dear Dr. Wicklin,
Why doesn't SYMPUT work in IML? In the DATA step, I can say CALL SYMPUT("MyMacro", 5) but this doesn't work in IML!
The SYMPUT subroutine does work in SAS/IML software! However, the second argument to SYMPUT must be a character value.
The SYMPUT subroutine is used to store a value in a macro variable. In your example, you are creating the macro variable MyMacro, which will contain the string "5". Macro variables always contain a string—never a numeric value.
As stated in the documentation, the second argument to the SYMPUT subroutine is "a character constant, variable, or expression." So why does your call work in the DATA step? The DATA step automatically converts character values to numeric values, and vice versa! For example, the following DATA step parses and runs without errors:
data a; x = 1 + "2"; /* convert char to numeric */ y = cos("0"); z = "B" || 4; /* convert numeric to char */ call symput("MyMacro", 5); run;
However, if you look in the SAS log, you will see the following NOTEs:
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 2:9 3:9 NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column). 4:12 5:24
The DATA step has "helped" you by converting between character and numeric expressions. Therefore, the SYMPUT call displays a NOTE, but not an error.
However, the SAS/IML language does not implicitly convert between character and numeric expressions. Instead, it supplies the NUM function and the CHAR function, which enable you to convert SAS/IML variables from one type to another. (You can also use the INPUTN and PUTN functions.) Therefore, in the SAS/IML language you should write
CALL SYMPUT("MyMacro", char(5));
If you really want automatic conversion, use the SYMPUTX subroutine. If you call SYMPUTX in the DATA step, no NOTE will be written to the log when the second argument is numeric. If you call SYMPUTX in SAS/IML software, the second argument is silently converted from a numeric to character value.
Thanks for writing!