Bendy
Bendy

Reputation: 3576

Is there a way to change the length of a variable using Proc datasets?

I have a lot of datasets and variables that I need to modify the attributes of. Everything is working fine EXCEPT for the below instance where I I need to change the length of a variable:

data inputdset ;
  format inputvar $20. ;
  inputvar='ABCDEFGHIJKLMNOPQRST' ;
run ;

proc datasets lib=work nolist memtype=data ;
  modify inputdset ;
    attrib inputvar format=$50. length=50 ;
  run ;
quit ;

Running this gives the following notes in the log:

NOTE: The LENGTH attribute cannot be changed and is therefore being ignored. Blockquote NOTE: MODIFY was successful for WORK.INPUTDSET.DATA.

...the final inputvar has a format of $50. as expected but still has a length of 20. Is there a way to have the length increased for these cases using proc datasets (or even better, if the length can be increased to match format)?

Upvotes: 2

Views: 6664

Answers (3)

shawn
shawn

Reputation: 56

To change the length of a column in a dataset you will have to rebuild the dataset with the new length. Typically you would do this using a datastep code pattern like the following(proc sql is another option).

data inputdset ;
  format inputvar $20. ;
  inputvar='ABCDEFGHIJKLMNOPQRST' ;
run ;

data inputdset;
length inputvar $ 50;
format inputvar $50.; * can change the format at the same time if you want;
set inputdset;
run;

The most common complaint with this pattern is that inputvar will now be the first column in the new dataset. You can correct this by properly listing all the variables in the length statement to preserve the original order.

Upvotes: 0

Reeza
Reeza

Reputation: 21264

Just a note that the documentation does specify that Length cannot be changed by the attrib statement under restrictions and the MODIFY statement.

https://support.sas.com/documentation/cdl/en/proc/68954/HTML/default/viewer.htm#n0ahh0eqtadmp3n1uwv55i2gyxiz.htm

MODIFY Statement

Changes the attributes of a SAS file and, through the use of subordinate statements, the attributes of variables in the SAS file.

Restriction: You cannot change the length of a variable using the LENGTH= option in an ATTRIB statement

Upvotes: 2

Quentin
Quentin

Reputation: 6378

It's always risky to say no, but I'm going to try it. PROC DATASETS can modify the metadata about a dataset, not the data stored in each record. Changing the length of a variable requires changing the value stored in every record for that variable (truncating it or lengthening it and padding with blanks). Thus changing the length of a variable requires rewriting the entire dataset, which can be done by the DATA step or PROC SQL, but not PROC DATASETS.

Upvotes: 7

Related Questions