变量的频率

时间:2016-09-19 09:57:19

标签: sas sas-macro

下面的

prg尝试计算变量的实例。

%macro freq(dsn=,variable=,freq=,label);

proc freq data = &dsn;
tables &variable;
run;

%mend;
%freq(dsn=fff,variable=ggg);

1 个答案:

答案 0 :(得分:0)

您应该可以通过添加格式和打印过程来完成此操作。我已经对此进行了测试,我相信它可以实现您想要实现的目标。

%macro freq(indsn=,variable=,freq=,label);

/* Create a new format or label when a value falls between
   0 and the user defined frequency. */
proc format;
    value fmt    0-&freq. = "&label.";
run;

/* Run the frequency procedure and suppress the output
   to a temporary data set. */
proc freq data = &indsn;
tables &variable / noprint out=temp;
run;

/* Print the temporary data set and format the Count
   variable (which was created in the freq procedure)
   to the format, fmt, that we created. Finally, print only
   records with a frequency less than the user defined
   frequency. */
proc print data=temp noobs;
    format count fmt.;
    where count le &freq.;
run;

%mend;

通过最近的编辑,您可以使用数据步骤和if语句完成此操作。

%macro freq(indsn=,variable=,freq=,label);

/* Run the frequency procedure and suppress the output
   to a temporary data set. */
proc freq data = &indsn;
tables &variable / noprint out=temp;
run;

/* Assign variable a new name if its frequency equals the
   user defined frequency and only keep records with a count
   less than or equal to the frequency. */
data temp;
    set temp;
if count le &freq.;
if count = &freq. then &variable. = &label.;
run;

/* Print only the &variable variable. */
proc print data=temp noobs;
var &variable.;
run;

%mend;