SAS:使用宏格式化多个proc频率

问题描述 投票:0回答:1

我工作的团队中没有其他分析师,并且对于同时运行多个proc freq的最有效方法有疑问。

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

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

%survey (question, formatA, formatB);

“问题”,“格式”和“格式”将是数据字符串,例如:

- “问题”将是KCI_1 KCI_2到KCI_80 - “formatA”将是KCI_1fmt KCI_2fmt到KCI_80fmt - “formatB”将是KCI_1fmt。 KCI_2fmt。通过KCI_80fmt。

macros sas formatting
1个回答
0
投票

丹妮尔:

您可以使用宏将已知格式分配给尚未格式化的变量。其余的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;
© www.soinside.com 2019 - 2024. All rights reserved.