美文网首页
【SAS Macros】修改数据集字符变量长度

【SAS Macros】修改数据集字符变量长度

作者: caokai001 | 来源:发表于2021-09-11 16:20 被阅读0次

    对数据集(如td1)中全部的字符变量设置统一的长度,以免出现字符串截断;

    P1:Code

    /*********************************************************************
    ***  Program: ms_char_set_len.sas
    ***  Programmer: Kai Cao
    ***  Date: 10Sep2021
    ***  Study: all study
    ***  Version: 1.0 
    ***  Input dataset: dataset len 
    ***  Output : __len_list
    ***  Description: Use SAS Macros to set uniform lengths for data set variables 
     
    
            dataset:   required. Data set whose length needs to be modified.
                                 such as td1 ;
            len    :   optional. Set the length of string variables uniformly.
                                 the default is 12000;
    *********************************************************************
    ***  MODIFICATIONS:
    ***  Programmer:
    ***  Date:
    ***  Reason:
    ***
    *********************************************************************/
    
    /*%let len=12000;*/
    /*%let dataset=ae2_;*/
    
    
    %macro ms_char_set_len(dataset=%str(td1),
                            len=%str(12000));
        /*create column-len mapping*/
        data null;
            set &dataset.;
            array char_vars{*} $&len. _character_;
            if _N_=1 then do ;
                do i= 1 to dim(char_vars);
                    __column=vname(char_vars{i});
                    __len=&len.;output; 
                end;
            end;
            keep __:;
        run;
    
        /*convert to macros vars*/
        proc sql noprint;
            select "length " ||strip(__column)||" $&len. ;" into : __len_list separated by " "
            from null
        ;
        quit;
    
        /*delect the work.null dataset*/
        proc sql noprint;
            drop table work.null;
        quit;
        
    %mend;
    
    /*%put &__len_list.;*/
    
    
    *---------------------------------------------------*
    * Example                                           *
    *---------------------------------------------------*;
    /*%include "/usrfiles/bgcrh/support/utilities/init/init_global.sas";*/
    /*libname uatdata_ "&import/irt_data" ;*/
    /**/
    /*proc copy in=uatdata_ out=work;*/
    /*    select td td1;*/
    /*run;*/
    /**/
    /*options mlogic mprint nosymbolgen;*/
    /*%ms_char_set_len(dataset=%str(td1)); *%ms_char_set_len(dataset=%str(td1),len=%str(100));*/
    /*data test2;*/
    /*   &__len_list.*/
    /*  set td1;*/
    /*run;*/
    

    P2:Usage:

    导入td1数据集,对字符变量设置恒定长度;

    %include "/usrfiles/bgcrh/build/training_project_1/practice_wh/cp/dev/pgm/vendor_recon/kai_cao/ms_char_set_len.sas";
    libname uatdata_ "&import/irt_data" ;
    
    proc copy in=uatdata_ out=work;
        select td td1;
    run;
    
    options mlogic mprint nosymbolgen;
    %ms_char_set_len(dataset=%str(td1)); *%ms_char_set_len(dataset=%str(td1),len=%str(100));
    data test2;
         &__len_list.
        set td1;
    run;
    

    P3:处理前:

    img

    P4:处理后:可以看到使用ms_char_set_len,字符变量会排在前面

    img

    Note:欢迎提出意见和建议进行完善✋~

    相关文章

      网友评论

          本文标题:【SAS Macros】修改数据集字符变量长度

          本文链接:https://www.haomeiwen.com/subject/ahhawltx.html