i have a dataset with more than 100 variables , I want to set the length of all character variables without changing their order. how to get this ?
@archita wrote:
i have a dataset with more than 100 variables , I want to set the length of all character variables without changing their order. how to get this ?
Do you want to change all variable to have the same length?
Do you know how to change the length of one variable?
@archita wrote:
i have a dataset with more than 100 variables , I want to set the length of all character variables without changing their order. how to get this ?
Do you want to change all variable to have the same length?
Do you know how to change the length of one variable?
proc sql seems to be a good choice to solve the problem.
/* create a dataset that can be modified */
data work.class;
set sashelp.class;
run;
/* the view will contain all char-variables ordered by their appearance in the dataset */
proc sql noprint;
create view work.vars as
select Name
from sashelp.vcolumn
where Libname = 'WORK'
and MemName = 'CLASS'
and Type = 'char'
order by Varnum
;
quit;
/* creates and executes a proc-sql-step setting the length of all char-variables to 42 - the generated code is written to the log */
data _null_;
set work.vars end= jobDone;
if _n_ = 1 then do;
call execute('proc sql noprint;');
call execute('alter table work.class');
call execute('modify ');
end;
call execute(catx(' ', Name, 'char(42)', ifc(not JobDone, ',', '')));
if jobDone then do;
call execute('; quit;');
end;
run;
@archita wrote:
I need it only by data step.
This is similar to @andreas_lds's approach:
data _null_;
call execute('data have; length');
do until(last);
set sashelp.vcolumn end=last;
where libname='WORK' & memname='HAVE';
call execute(catx(' ',name,ifc(type='char','$42',put(length,1.))));
end;
call execute('; set have; run;');
stop;
run;
Should all character variables have the same length?
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.