03-05-2018 11:39 PM
Below is a simple macro I need to create for an assignment. But I am having an issue that I do not understand.
The data is called homework.cereal and the name of it is assigned to a macro variable &name. It has a bunch of cereals, the manufacturer of the cereal, 12 numeric variables describing each cereal such as amount of protein, sugar etc. .
The macro needs to be able to take any of the the numeric variables from the data set, for example sugar. And then it needs to create a macro variable for each brand where the value assigned to it is the mean amount of sugar for that brand.
Everything in the macro seems to be good except that I keep getting errors that the name of the macro can't have a symbol it it. So it is reading &variable in the first argument of call symput as it is instead of putting in 'sugar'. How do i fix this?
%macro create_macros(variable) ;
title "Means of &variable by Manufacturer" ;
proc means data = &name ;
var &variable ;
output out = stats mean = ;
error message :
NOTE: Invalid argument to function SYMPUT('&variable_P'[12 of 25 characters shown],'
144.375') at line 232 column 175.
Manufacturer=Post _TYPE_=1 _FREQ_=8 Sodium=144.375 _ERROR_=1 _N_=6
ERROR: Symbolic variable name &VARIABLE_QUAKEROATS must contain only letters, digits, and
03-06-2018 03:07 AM - edited 03-06-2018 03:10 AM
Take a close look at the contents of your manufacturer variable (leading blanks, UTF characters etc), and make sure that your log corresponds to your code. Those single quotes in the log look suspicious.
Your code as such is valid, see this example adapted to sashelp.class:
%macro create_macros(variable) ; title "Means of &variable by Manufacturer" ; proc means data = &name ; class sex; var &variable ; output out = stats mean = ; run; data _null_; set stats; call symput("&variable"||sex!!' ',&variable); run; %mend create_macros; %let name=sashelp.class; %create_macros(weight);
03-06-2018 04:18 AM
Two issues. First, PROC MEANS should add the NWAY option. Otherwise, you will need to deal with an additional observation that contains statistics for all manufacturers combined.
Second, the program you are showing is not the one you ran. You ran something more like this:
The underscore is up to you, but the single quotes are causing the problem. Switch to double quotes, the way you did for the title statement. And switching to CALL SYMPUTX can only help by eliminating leading blanks in the values assigned to the macro variables.
If you have already made these changes, the problem lies in SAS being able to locate the newest version of the macro. If that is the problem, the simplest way to solve that is to end the current SAS session and start a new one.