본문 바로가기
SAS/SAS Tip's

[변수출력] 다양한 변수 리스트 출력 방식;

by 기서무나구물 2021. 11. 15.

포스팅 목차

    [변수출력] 다양한 변수 리스트 출력 방식;

     

    * Proc SQL 구문의 feedback 옵션

    Proc SQL 구문의 feedback 옵션은 SELECT * 에서 전체 변수(*)에 해당하는 모든 변수들을 LOG 화면에 출력.;

    SAS Statements
    proc sql feedback; 
      select * 
      from   SASHELP.CLASS; 
    run;

     

    Results (LOG 화면)
    select CLASS.Name, CLASS.Sex, CLASS.Age, CLASS.Height, CLASS.Weight
    from   SASHELP.CLASS;

     


    * PROC CONTENTS 프로시져;

    SAS Statements
    PROC CONTENTS DATA=SASHELP.CLASS OUT=BACK POSITION;
    RUN;

     

    Results

     


    * PROC SQL의 DICTIONARY.COLUMNS 테이블;

    SAS Statements
    PROC SQL;
      SELECT *
      FROM   DICTIONARY.COLUMNS
      WHERE  LIBNAME = 'SASHELP'
        AND  MEMNAME = 'CLASS';
    QUIT;

     

    Results

     


    * SASHELP.VCOLUMN VIEW 테이블;

    SAS Statements
    DATA CLASS_VAR;
    SET SASHELP.VCOLUMN;
         WHERE  LIBNAME = 'SASHELP'
           AND  MEMNAME = 'CLASS';
    RUN;

     

    Results


    * 로그 화면에 데이터 Export를 위한 FILE 구문 자동 생성;

    SAS Statements
    PROC EXPORT DATA= SASHELP.CLASS 
                OUTFILE= "D:\BACK.csv" 
                DBMS=CSV
                REPLACE;
         PUTNAMES=YES;
    RUN;

     

    Results (LOG 화면)
     75          /**********************************************************************
     76          *   PRODUCT:   SAS
     77          *   VERSION:   9.4
     78          *   CREATOR:   External File Interface
     79          *   DATE:      14NOV21
     80          *   DESC:      Generated SAS Datastep Code
     81          *   TEMPLATE SOURCE:  (None Specified.)
     82          ***********************************************************************/
     83             data _null_;
     84             %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
     85             %let _EFIREC_ = 0;     /* clear export record count macro variable */
     86             file 'BACK.csv' delimiter=',' DSD DROPOVER lrecl=32767;
     87             if _n_ = 1 then        /* write column names or labels */
     88              do;
     89                put
     90                   "Name"
     91                ','
     92                   "Sex"
     93                ','
     94                   "Age"
     95                ','
     96                   "Height"
     97                ','
     98                   "Weight"
     99                ;
     100             end;
     101           set  SASHELP.CLASS   end=EFIEOD;
     102               format Name $8. ;
     103               format Sex $1. ;
     104               format Age best12. ;
     105               format Height best12. ;
     106               format Weight best12. ;
     107             do;
     108               EFIOUT + 1;
     109               put Name $ @;
     110               put Sex $ @;
     111               put Age @;
     112               put Height @;
     113               put Weight ;
     114               ;
     115             end;
     116            if _ERROR_ then call symputx('_EFIERR_',1);  /* set ERROR detection macro variable */
     117            if EFIEOD then call symputx('_EFIREC_',EFIOUT);
     118            run;

     


    * VARNAME 함수를 사용하여서 변수 리스트 출력;


    https://statwith.tistory.com/2857;

     

    SAS Statements
    DATA _NULL_;
         dsid=OPEN("sashelp.class");
         VAR1 = varname(dsid,1);
         VAR2 = varname(dsid,2);
         VAR3 = varname(dsid,3);
         VAR4 = varname(dsid,4);
         VAR5 = varname(dsid,5);
         PUT VAR1= VAR2= VAR3= VAR4= VAR5=;
         rc = close(dsid);
    RUN;

     

    Results (LOG 화면)
     VAR1=Name VAR2=Sex VAR3=Age VAR4=Height VAR5=Weight

     


    * DO 반복문을 사용하여서 변수 리스트 출력 및 변수 생성;

    SAS Statements
    data VARNAME;
         length varlist $ 80;
         tableid = open('SASHELP.CLASS','i');
         varlist=' ';
    
         do i = 1 to min(5,attrn(tableid,'nvars'));
            varlist = CATX(' ',varlist,varname(tableid,i));
         end;
    
         put varlist=;
         rc = close(tableid);
    run;

     

    Results
    OBS             varlist              tableid    i    rc
    
    1     Name Sex Age Height Weight       1       6     0
    반응형

    댓글