simple create macro function

Reply
Occasional Contributor
Posts: 9

simple create macro function

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 ;
class manufacturer;
var &variable ;
output out = stats mean = ;
run;
data _null_;
set stats;
call symput("&variable"||manufacturer,&variable);
run;
%mend create_macros;

 

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
underscores.

Moderator
Posts: 325

Re: simple create macro function

Posted in reply to UCFtigers2017
First thoughts - use symputx instead of symput. This removes any leading & trailing blanks which may be invalidating the macro name.
Super User
Posts: 10,239

Re: simple create macro function

[ Edited ]
Posted in reply to UCFtigers2017

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);

which works.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 9

Re: simple create macro function

Posted in reply to KurtBremser
Thank you!
Super User
Posts: 6,774

Re: simple create macro function

Posted in reply to UCFtigers2017

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:

 

call symput('&variable'||'_'||manufacturer,&variable);

 

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.

Occasional Contributor
Posts: 9

Re: simple create macro function

Posted in reply to Astounding

Thank you !

Moderator
Posts: 325

Re: simple create macro function

Posted in reply to UCFtigers2017
If one of these suggestions helped you fix your problem, please return to this page and select "Accept as Solution" for that suggestion.
Ask a Question
Discussion stats
  • 6 replies
  • 134 views
  • 2 likes
  • 4 in conversation