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;
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;
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;
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.
Hi Patrick,
How does the SQL method compare efficiency wise to using a Datastep?
Regards,
Scott
Since both do a sequential read/write of the table, comparative performance should not be an issue.
The SQL approach will maintain all table attributes like indexes so it's imho the cleanest way of coding a change of variable lengths.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.