here's a solution use this macro /* change a variable's length without change the position the parads should be defined as below do not change the parads's variable data parads; length name $32 length 8; name = "a2";length = 8;output; name = "a7";length = 200;output; run; */ %macro rclen(ids, parads); *get all variable names; proc sql noprint; create table _cvars as select upcase(name) as name, length, varnum ,type from sashelp.vcolumn where libname='WORK' and memname=upcase("&ids.") order by name; quit; data ¶ds.; set ¶ds.; name = upcase(name); run; proc sort data=¶ds.;by name;run; data _cvars; merge _cvars ¶ds.; by name; run; proc sort data=_cvars;by varnum;run; %local i dsid nobs rc name tempStr resStr; %let dsid = %sysfunc(open(_cvars, i)); %let nobs = %sysfunc(attrn(&dsid,NOBS)); %do i=1 %to &nobs.; %let rc = %sysfunc(fetchobs(&dsid,&i)); %let name=%sysfunc(getvarc(&dsid, %sysfunc(varnum(&dsid,name)))); %let type=%sysfunc(getvarc(&dsid, %sysfunc(varnum(&dsid,type)))); %let length=%sysfunc(getvarn(&dsid, %sysfunc(varnum(&dsid,length)))); %if &type.=char %then %do; %let tempStr = $&length.; %end; %else %do; %let tempStr = 8; %end; %let resStr = &resStr. &name. %left(%trim(&tempStr.)) ; %end; %let rc=%sysfunc(close(&dsid)); %put &resStr.; OPTIONS VARLENCHK= NOWARN; data &ids.; length &resStr.; set &ids.; run; OPTIONS VARLENCHK= WARN; %mend rclen; here's the test code data a; length a1 8 a2 $100 a3 8; a1 = 1; a2 = "ad"; a3 = 1; a4 = 8; a5 = 8; a6 = 8; a7 = "asdfs"; a8 = 8; a9 = 8; run; data parads; length name $32 length 8; name = "a2";length = 8;output; name = "a7";length = 200;output; run; %rclen(a, parads); hope you like it
... View more