对数据集(如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;
网友评论