Help using Base SAS procedures

Change All Character variables' length

Reply
Contributor
Posts: 44

Change All Character variables' length

I have a dataset with 200 character variables. all of those characters variables' lengths are between 5 to 10. Is there anyway to change the length of all of these character variables' to $40 in a short efficent way?( i know how to chnage the length or attribute, before the set statmment, but it will create a very long ugly coding)

Thank you a lot

Respected Advisor
Posts: 3,124

Re: Change All Character variables' length

I suppose you will need some codgen:

proc sql;

     select name into :vname separated by ' ' from dictionary.columns where LIBNAME='SASHELP' AND MEMNAME='CLASS' AND type='char';

quit;

data want;

     length &vname $ 40;

     set sashelp.class;

run;

Haikuo

Contributor
Posts: 44

Re: Change All Character variables' length

sorry did not understand how to relate your codes with my dataset.

Respected Advisor
Posts: 3,124

Re: Change All Character variables' length

proc sql;

     select name into :vname separated by ' ' from dictionary.columns where LIBNAME='YOURLIB' /*NEED TO BE CAPPED*/ AND MEMNAME='YOURDATASET'/*NEED TO BE CAPPED*/  AND type='char';

quit;

data want;

     length &vname $ 40;

     set YOURLIB.YOURDATASET;

run;

Contributor
Posts: 44

Re: Change All Character variables' length

Hi Hai,

Please find the log as below (bold and underlined) : SQL staement do not produce any rows. I am sure you are correct But i am missing something. 

690 proc sql;

691 select name into :vname separated by ' ' from dictionary.columns where LIBNAME='YOURLIB'

691! /*NEED TO BE CAPPED*/ AND MEMNAME='YOURDATASET'/*NEED TO BE CAPPED*/ AND type='char';

NOTE: No rows were selected.

692 quit;

NOTE: PROCEDURE SQL used (Total process time):

real time 0.14 seconds

cpu time 0.01 seconds

 

693

694

695 data want;

696 length &vname $ 40;

697 set tto;

698 run;

NOTE: Variable Name is uninitialized.

NOTE: Variable Sex is uninitialized.

NOTE: There were 12 observations read from the data set WORK.TTO.

NOTE: The data set WORK.WANT has 12 observations and 26 variables.

NOTE: DATA statement used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds

Respected Advisor
Posts: 3,124

Re: Change All Character variables' length

proc sql;

     select name into :vname separated by ' ' from dictionary.columns where LIBNAME='WORK' /*NEED TO BE CAPPED*/ AND MEMNAME='TTO'/*NEED TO BE CAPPED*/  AND type='char';

quit;

data want;

     length &vname $ 40;

     set TTO;

run;

Good luck,

Haikuo

Contributor
Posts: 44

Re: Change All Character variables' length

Thanks Haikuo. It workd. Thanks a lot

Super User
Posts: 9,671

Re: Change All Character variables' length

HaiKuo's code will overwrite the table again , that would not be an efficient way .

data class;
 set sashelp.class;
run;
 proc sql;
     select catx(' ',name,'char(40)') into :vname separated by ',' from dictionary.columns where LIBNAME='SASHELP' AND MEMNAME='CLASS' AND type='char';
alter table class
modify &vname ;
quit;

Xia Keshan

Ask a Question
Discussion stats
  • 7 replies
  • 283 views
  • 0 likes
  • 3 in conversation