Help using Base SAS procedures

Length

Reply
Contributor
Posts: 74

Length

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
Super Contributor
Super Contributor
Posts: 365

Re: Length

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
New Contributor
Posts: 2

Re: Length

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;
Respected Advisor
Posts: 3,799

Re: Length

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=xSmiley Happy;
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]
Regular Contributor
Posts: 171

Re: Length

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]
Regular Contributor
Posts: 241

Re: Length

@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

   ...

   */
Contributor
Posts: 74

Re: Length

Posted in reply to chang_y_chung_hotmail_com
Thank you all for your suggestions.
Ask a Question
Discussion stats
  • 6 replies
  • 192 views
  • 0 likes
  • 6 in conversation