Change variable length in SAS dataset

sas
19,070

Solution 1

If you put your LENGTH statement before the SET statement, in a Data step, you can change the length of a variable. Obviously, you will get truncation if you have data longer than your new length.

However, using a DATA step to change the length is also re-creating the data set, so I'm confused by that part of your question.

Solution 2

Length of a variable remains same once you set the dataset. Add length statements before you set the dataset if you need to change length of a columns

data a;
  length a, b, c $200 ;
  set b ;
run ;

Solution 3

The only way to change the length of a variable in a datastep is to define it before a source (SET) dataset is read in.

Conversely you can use an alter statement in a proc sql. SAS support alter statement

Share:
19,070
DonnieCrump
Author by

DonnieCrump

please delete me

Updated on June 04, 2022

Comments

  • DonnieCrump
    DonnieCrump almost 2 years

    I need to change the variable length in a existing dataset. I can change the format and informat but not the length. I get an error. The documentation says this is possible but there are no examples.

    Here is my issue. My data source could change so I don't want to pre define columns on import. I want to do a generic import and then look for certain columns and adjust the length.

    I have tried PROC SQL and DATA steps. It looks like the only way to do this is to recreate the dataset or the column. Which I don't want to do.

    Thanks, Donnie

  • thelatemail
    thelatemail over 7 years
    A trap for young players - this will not automatically change the format of a variable to match the new length, which may actually end up 'hiding' the assignment of long strings in the new dataset. So it can be worth changing the length and format at the same time so everything matches up neatly.