变量的频率

frequency of the variable

下面的 prg 尝试计算变量的实例。

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

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

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

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

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