Changing variable length without creating a new dataset

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Changing variable length without creating a new dataset

Is possible to change variable length without creating a new dataset using proc dataset or sql?

Example:

proc datasets library=work nolist;

modify datset

format variabe $5

length variable 10;

run;

Proc sql;

alter table dataset

modify variabe format = $5

modify length variable 10;

quit;


Accepted Solutions
Solution
‎04-03-2014 07:25 PM
Super Contributor
Posts: 297

Re: Changing variable length without creating a new dataset

Unfortunately there is no way to modify the length of a variable using the proc datasets modify function, however on the bright side you can use it to alter the format.

data have;

attrib var1 format = $15.;

var1 = '1234567890';

run;

proc contents data=have;

run;

proc datasets library=work nolist;

modify have;

format var1 $4.;

run;

proc contents data=have;

run;

View solution in original post


All Replies
Solution
‎04-03-2014 07:25 PM
Super Contributor
Posts: 297

Re: Changing variable length without creating a new dataset

Unfortunately there is no way to modify the length of a variable using the proc datasets modify function, however on the bright side you can use it to alter the format.

data have;

attrib var1 format = $15.;

var1 = '1234567890';

run;

proc contents data=have;

run;

proc datasets library=work nolist;

modify have;

format var1 $4.;

run;

proc contents data=have;

run;

Respected Advisor
Posts: 3,895

Re: Changing variable length without creating a new dataset

Changing variable attributes like format and informat needs only a change of the descriptor part of a data set. This can be done by either Proc Datasets or Proc SQL Alter table.

For  changing the variable length the data set needs to be re-written as this also changes how the data gets physically stored (more or less space required). This can not be done using Proc Datasets but it can be done using PROC SQL ALTER TABLE / MODIFY. Be aware that if you change the length the full data set gets re-written so this could take a bit of time for a big table.

Super Contributor
Posts: 297

Re: Changing variable length without creating a new dataset

Hi Patrick,

How does the SQL method compare efficiency wise to using a Datastep?

Regards,

Scott

Super User
Posts: 6,946

Re: Changing variable length without creating a new dataset

Since both do a sequential read/write of the table, comparative performance should not be an issue.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,895

Re: Changing variable length without creating a new dataset

The SQL approach will maintain all table attributes like indexes so it's imho the cleanest way of coding a change of variable lengths.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 270 views
  • 4 likes
  • 4 in conversation