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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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