SAS:使用宏来格式化多个PROC FREQ



我在工作中没有另一个分析师,并且对同时运行多个Proc Freq的最有效方法有疑问。

我的目标是运行大约160个不同的频率,并包括所有这些频率。我认为宏是最快的方法,但是我只有基本宏的经验。以下是我的思考过程,假设数据已经格式化:

%macro survey(question, formatA formatB);
proc freq;
table &question;
format &formatA &formatB;
%mend;
%survey (question, formatA, formatB);

"问题","格式"one_answers"格式化"将是数据字符串:

- "问题"将是kCi_1 kCi_2通过kCI_80 - "格式"将通过KCI_1FMT KCI_2FMT通过KCI_80FMT - "格式化"将是KCI_1FMT。KCI_2FMT。通过KCI_80FMT。

danielle:

您可以使用宏将已知格式分配给尚未格式化的变量。FREQ的其余部分不必是宏观化的。

* make some survey data with unformatted responses;
data have;
  do respondent_id = 1 to 10000;
     array responses KCI_1-KCI_80;
     do _n_ = 1 to dim(responses);
       responses(_n_) = ceil(4*ranuni(123));
     end;
     output;
  end;
run;
* make some format data for each question;
data responseMeanings;
  length questionID 8 responseValue 8 responseMeaning $50;
  do questionID = 1 to 80;
    fmtname = cats('Q',questionID,'_fmt');
    peg = ranuni (1234); drop peg;
    do responseValue = 1 to 4;
      select;
        when (peg < 0.4) responseMeaning = scan('Never,Seldom,Often,Always', responseValue);
        when (peg < 0.8) responseMeaning = scan('Yes,No,Don''t Ask,Don''t Tell', responseValue);
        otherwise responseMeaning = scan('Nasty,Sour,Sweet,Tasty', responseValue);
      end;
      output;
    end;
  end;
run;
* create a custom format for the responses of each question;
proc format cntlin=responseMeanings(rename=(responseValue=start responseMeaning=label));
run;
* macro to associate variables with the corresponding custom format;
%macro format_each_response;
  %local i;
  format
    %do i = 1 %to 80;
    KCI_&i Q&i._fmt.
    %end;
  ;
%mend;
* compute frequency counts;
proc freq data=have;
  table KCI_1-KCI_80;
  %format_each_response;
run;

最新更新