如何为一组全局宏变量的条件创建(if…then…)编写简洁的代码

How to write concise code for the conditional creation (if…then…) of a set of global macro variables

我创建了 14 个全局宏变量,如下所示:

data _NULL_;
set &medf;
if &class1='Catcher' then call symputx('MedianC',med);
if &class1='Catcher' then call symputx('FrequencyC',_FREQ_);
if &class1='First Baseman' then call symputx('MedianFB',med);
if &class1='First Baseman' then call symputx('FrequencyFB',_FREQ_);
if &class1='Outfielder' then call symputx('MedianO',med);
if &class1='Outfielder' then call symputx('FrequencyO',_FREQ_);
if &class1='Pitcher' then call symputx('MedianP',med);
if &class1='Pitcher' then call symputx('FrequencyP',_FREQ_);
if &class1='Second Baseman' then call symputx('MedianSB',med);
if &class1='Second Baseman' then call symputx('FrequencySB',_FREQ_);
if &class1='Shortstop' then call symputx('MedianS',med);
if &class1='Shortstop' then call symputx('FrequencyS',_FREQ_);
if &class1='Third Baseman' then call symputx('MedianTB',med);
if &class1='Third Baseman' then call symputx('FrequencyTB',_FREQ_);
run;

这似乎是一个低效的代码,所以我想知道如何才能更简洁地编写它。我查看了 CALL SYMPUTX 的各种用途,似乎我可能不需要 14 行代码来表示 14 个全局宏变量(即,一行 CALL SYMPUTX 可能会产生多个宏变量)。但是,我不确定如何在更少的代码行中保留变量创建的条件性质。

如果有人能提供一些指导,我将不胜感激。谢谢!

假设每个&class1'word'的首字母可以组成后缀放在宏变量名的末尾,就可以完成这个任务而不需要if-then逻辑:

data _NULL_;
set &medf;
length suff ;
i=1;
/* Do loop pulls together the first letter of each word */
/* in the &class1 variable value into a new variable called suff */
do while (scan(&class1,i) ^= '');
  suff=cats(suff,substr(scan(&class1,i),1,1));
  i+1;
end;

/* The Median and Frequency words joined to suff make the new macro variable name, */ 
/* the second argument just holds the values */
call symputx(cats('Median',suff),med);
call symputx(cats('Frequency',suff),_FREQ_);

run;