导航

/*统计表的字段*/
PROC CONTENTS DATA=SASHELP.CLASS NOPRINT OUT=CA(KEEP=NAME);
    
RUN;

/*提取表的变量名*/
PROC SQL NOPRINT;
SELECT NAME INTO:A SEPARATED BY ' ' FROM CA;
RUN;
QUIT;


%LET I=1;

/*计算各个变量的频数,并存放在一个数据集中*/

%MACRO M_A();

    PROC FREQ DATA=SASHELP.CLASS;
        %DO %UNTIL(%SCAN(&A.,&I,' ')=);

            %LET NI=%SCAN(&A,&I,' ');
            TABLES &NI/MISSING OUT=&NI(RENAME=(&NI=NAME)) NOPRINT;

            %LET i = %eval(&i.+1);

            %PUT "&I.=" &NI.;
        
        %END;
    RUN;

    %LET I=1;
    %DO %UNTIL(%SCAN(&A.,&I,' ')=);

        %LET NI=%SCAN(&A,&I,' ');
        
        DATA &NI.;
            LENGTH VAR $30. VAL $40.;
            SET &NI.;
            VAR="&NI.";
            VAL=CATS(NAME,'');
            DROP NAME;
        RUN;
        PROC APPEND BASE=D DATA=&NI.;
        RUN;

        %LET i = %EVAL(&i.+1);

        %PUT "&I.=SSSSSSS" &NI.;

    %END;

    PROC PRINT DATA=D;
    RUN;

%MEND;

%M_A();