使用 Proc Freq 变量的输出作为 Proc 格式的输入

Using output of Proc Freq variables for input into Proc Format

我想获取变量级别的数量以及唯一标识符输出的变量,但目前我的方法不起作用。然后我想使用来自 proc freq 的唯一 ID 和关联编号 1-num_levels。

这是我的 proc freq:

PROC FREQ DATA=my_data (keep=IDs) nlevels;
table all/out=out_data;
%let dim=levels;
%let IDs;
run;

然后我尝试使用宏变量,但它没有用,所以我包括了我的 proc 格式的手动版本,以便更好地了解我想要实现的目标,但希望能使其更加自动化.

PROC FORMAT; 
INVALUE INDEX
"1234" = 1
"2345" = 2
.
.
.
"8901" =25;
/*25 represents the output of the levels 
variable from proc freq but I couldn't figure out how to grab that either*/
RUN;

如有任何帮助,我们将不胜感激。 谢谢!

这是一个完整的解决方案,它说明了执行此操作的 PROC FORMAT CNTLIN 方法。这里的想法是用观察号来掩盖名字。

*Create list of unique names;
proc freq data=sashelp.class noprint;
    table name/out = mask;
run;

*create control data set. Variables that need to be set are:
   fmtname, start, label and type;

data name_fmt;
    set mask;
    fmtname = 'namefmt';
    type='J';

    *J specified character informat, C would be character format;
    start=name;
    label = put(_n_, z2.); *Use the row number as the recoded value;
run;

*create the format;
proc format cntlin=name_fmt;
run;

*Sample usage;
data class;
    set sashelp.class;
    name_masked = input(name, $namefmt.);
    drop name;
run;