I have a SAS dataset, let's call it A. It has both numeric and character variables. I wish to set the length of ALL character variables to 300. How should I do it ? I tried arrays, it did not work. Thank you in advance.
0
votes
1 Answers
2
votes
Once you have a dataset you can't change the variable definitions. But you can use the metadata about the dataset to generate code that can make a new dataset with different settings. If you don't care about the variable order then just pull the names into a space delimited list and use it in a LENGTH statement.
proc sql noprint;
select name into :names separated by ' '
from dictionary.columns where libname='WORK' and memname='A' and type='char'
;
quit;
data want;
length &names $300;
set A;
run;
If you do care about the order then include the numeric variables as well. You can eliminate the warning about ordering by a variable not included in the select statement by also pulling VARNUM and ignoring it by stuffing it into the same macro variable that will be overwritten with the list of names and lengths.
proc sql noprint;
select varnum
, catx(' ',name,case when (type='char') then '$300' else '8' end)
into :names
, :names separated by ' '
from dictionary.columns where libname='WORK' and memname='A'
order by varnum
;
quit;
data want;
length &names ;
set A;
run;