BookmarkSubscribeRSS Feed
ren2010
Obsidian | Level 7
Hi,

I have a dataset with more than 100 variables and I want to change the length of two character vars .I thought of using SQL(by listing all the 100 vars,so that i can maintain the order of the vars in original dataset),but just want to know any other method I can use to change length and also maintain the order of variables in the dataset.

Please advice

Thanks
6 REPLIES 6
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Ren2010,

You can use variable lists. Let we have variables i, z, g, p and would like to change length of g then
[pre]
data i;
i=1; z=15.; g="3."; p="14";
run;
proc SQL;
select name as lst into :lst separated " " from SASHELP.VCOLUMN
where LIBNAME="WORK" and MEMNAME="I"
;quit;
%put lst=&lst;
data r;
length g $5;
set i;
run;
data rr;
retain &lst;
set r;
run;
[/pre]
Sincerely,
SPR
GG44
Calcite | Level 5
Hello ren2010,
You can also use proc datasets to modify the length of the variables. It will still maintain the order of the variables.
Example:
data a;
input name $ class $ dept $ designation $;
cards;
George IV computers Comp_dev
;
run;
proc contents data =a;
run;

data b;
retain class dept designation name;
set a;
run;

proc datasets lib=work nolist;
modify b;
format name $20.;
quit;
run;
proc contents data =b;
run;
data_null__
Jade | Level 19
Use "SAS Variable Lists" and a few well chosen unexecuted statements.

[pre]
data have;
length a b c 8 chr1 $10 x1-x5 chr2 $5 d e f 8;
retain _numeric_ 10;
retain _char_ 'abc';
do _n_ = 1 to 10;
output;
end;
run;
proc contents data=need varnum;
run;

data need;
if 0 then set have(keep=a--c);
length chr1 $20;
if 0 then set have(keep=x:);
length chr2 $40;
if 0 then set have;
stop;
run;

data need;
if 0 then modify need;
set have;
output;
run;

proc contents data=need varnum;
run;
[/pre]
polingjw
Quartz | Level 8
Here is another method that utilizes call vnext to generate a length statement. This example changes the length of the variable Sex from 1 to 2 in sashelp.class.

[pre]
data _null_;
if 0 then set sashelp.class;
length length_statement $ 32767 vname $32 vtype $1;
retain length_statement;
length_statement = 'length';
do while (vname ne 'length_statement');
call vnext(vname, vtype, vlength);
if vname = 'Sex' then vlength = 2;
if vname ne 'length_statement' then do;
var_length = catx(' ', vname, ifc(vtype='C', '$', ''), vlength);
length_statement = catx(' ', length_statement, var_length);
end;
end;
length_statement = cats(length_statement, ';');
call symputx("length_statement", length_statement);
run;

data class;
&length_statement
set sashelp.class;
run;
[/pre]
chang_y_chung_hotmail_com
Obsidian | Level 7
@ren2010: here is an (sql) way:



   /* test data */

   data have;

      length a b c 8 chr1 $10 x1-x5 chr2 $5 d e f 8;

      retain _numeric_ 10;

      retain _char_ 'abc';

      do _n_ = 1 to 1e6;

         output;

      end;

   run;

 

   /* change the length of a couple of variables */

   proc sql;

      alter table have modify chr1 char(20), chr2 char(4);

   quit;

 

   /* check */

   proc contents data=have;

   run;

   /* on log

    #    Variable    Type    Len

   ...

    4    chr1        Char     20

   10    chr2        Char      4

   ...

   */
ren2010
Obsidian | Level 7
Thank you all for your suggestions.

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

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2372 views
  • 0 likes
  • 6 in conversation