BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
archita
Fluorite | Level 6

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 ?

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

@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?

View solution in original post

7 REPLIES 7
andreas_lds
Jade | Level 19

@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
Fluorite | Level 6
i want to change all the variable to same length without changong their order
andreas_lds
Jade | Level 19

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
Fluorite | Level 6
I need it only by data step.
FreelanceReinh
Jade | Level 19

@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;
PeterClemmensen
Tourmaline | Level 20

Should all character variables have the same length?

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 7 replies
  • 1713 views
  • 1 like
  • 4 in conversation