DATA Step, Macro, Functions and more

Changing the length of a character variable

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

Changing the length of a character variable

Is there a way to change the length of a character variable?  I am using proc import to import

data from a .dbf file.  One of the data fields is character and 5 characters wide.  I would like

to later assign larger character strings to this variable.  To do this I create a new variable in a

separate data step to do this.  Is there more elegant way?


Accepted Solutions
Solution
‎09-23-2011 10:41 PM
SAS Employee
Posts: 104

Changing the length of a character variable

It's probable that, when you imported the data from dBase,  PROC IMPORT assigned a permanent format of $5. to the NAME variable.  If that is true, this code:

data have;
  length Name $ 30;
  set have;
  if _n_ eq 1 then Name="Rumpelstiltskin";
run;

actually DID change the length of the variable, but it was only displaying 5 characters by default, because of the format.  Subsequently running this code:

data have;
  format Name $30.;
  set have;
  if _n_ eq 1 then Name="Rumpelstiltskin";
run;

would replace the $5. format with $30. and allow the extra characters to display. 

This PROC SQL solution simultaneously modifies both the length of the variable and the format:

proc sql;
alter table lib.have
  modify TextVar char(20) format=$20.;
quit;

Alternatively, you could use both LENGTH and FORMAT statements in the DATA step code to accomplish the same result:

data have;
  length Name $30;
  format Name $30.;
  set have;
  if _n_ eq 1 then Name="Rumpelstiltskin";
run;

View solution in original post


All Replies
PROC Star
Posts: 7,363

Changing the length of a character variable

Include a length statement before your set statement.  e.g.,

data have;

  set sashelp.class;

run;

proc contents;

run;

data have;

  length Name $30;

  set have;

  if _n_ eq 1 then Name="Rumpelstiltskin";

run;

proc contents;

run;

Contributor
Posts: 44

Changing the length of a character variable

Thanks Art.  That is what I was looking for.  However, in order for me to make this

work I hand to change "length" to "format".  Using "length" didn't allow me to reset

any character variable to hold longer strings.  Here is some sample code showing

what I had to do:

data have;

  format Name $30.;

  set have;

  if _n_ eq 1 then Name="Rumpelstiltskin";

run;

PROC Star
Posts: 7,363

Changing the length of a character variable

Out of curiosity, what version of SAS are you using and on which platform?

For me, on 9.2 on an XP, both length and format change the other accordingly.

Contributor
Posts: 44

Changing the length of a character variable

Hi Art,

We are running SAS 9.2 on Windows server 2008.

PROC Star
Posts: 7,363

Changing the length of a character variable

When you used the length statement did you make sure that it wasn't expressed as a format?  i.e., did you use $30 and NOT $30. ?

Contributor
Posts: 44

Changing the length of a character variable

That is correct.  The length statement I tried was:

length wingband $6;

When I changed that to:

format wingband $6.;

it worked for me.

PROC Star
Posts: 7,363

Changing the length of a character variable

Glad it worked!  Unless someone more knowedgeable than me can quickly provide an answer regarding why the length statement didn't work, I'll see if I can find out.

Frequent Contributor
Posts: 104

Changing the length of a character variable

All of my testing with SAS 9.1.3 PC and SAS 9.2 Unix shows length statement working properly.  Of course, the length statement needs to be at the beginning of the data step prior to the set statement.

I am at a loss to understand why it wouldn't work without seeing the actual program and logs.

Solution
‎09-23-2011 10:41 PM
SAS Employee
Posts: 104

Changing the length of a character variable

It's probable that, when you imported the data from dBase,  PROC IMPORT assigned a permanent format of $5. to the NAME variable.  If that is true, this code:

data have;
  length Name $ 30;
  set have;
  if _n_ eq 1 then Name="Rumpelstiltskin";
run;

actually DID change the length of the variable, but it was only displaying 5 characters by default, because of the format.  Subsequently running this code:

data have;
  format Name $30.;
  set have;
  if _n_ eq 1 then Name="Rumpelstiltskin";
run;

would replace the $5. format with $30. and allow the extra characters to display. 

This PROC SQL solution simultaneously modifies both the length of the variable and the format:

proc sql;
alter table lib.have
  modify TextVar char(20) format=$20.;
quit;

Alternatively, you could use both LENGTH and FORMAT statements in the DATA step code to accomplish the same result:

data have;
  length Name $30;
  format Name $30.;
  set have;
  if _n_ eq 1 then Name="Rumpelstiltskin";
run;
Contributor
Posts: 44

Changing the length of a character variable

Since the data is being imported from a dBase file your explanation must be correct.

Thanks for your help.

PROC Star
Posts: 7,363

Changing the length of a character variable

Wes,

If you have such a file that was imported from dBase, and doesn't contain confidential info and hasn't been modified, I'd appreciate your sending me a copy (to art297 at rogers dot com).  I'm still investigating the matter, but have never confronted a "permanent format".  I would bet that there is still more to learn.

Super User
Super User
Posts: 6,500

Changing the length of a character variable

Art -

  "Permanent formats" are just formats that are permanently attached because they are defined in the datasets. As opposed to formats that you temporarily attach during a procedure by using a format statement.

- Tom

PROC Star
Posts: 7,363

Changing the length of a character variable

Tom,

Thanks for enlightening me!  What I am concerned about is NOT that one has to define desired length and format changes but, instead, the behavior of the SAS Explorer.  When one only changes the length, it shows that both the length, format and informat has changed.

That wouldn't qualify as a "bug" but, frankly, it is simply wrong!  Why give a user incorrect information?

Super User
Super User
Posts: 6,500

Changing the length of a character variable

Art -

   I think that the problem is that ViewTable does not distinguish between the formats that are defined in the dataset and those that it is using because of default behaviour.   I would prefer that it just leave the format/informat attributes empty in that screen, as they are in the dataset.  If they must put in a default value then it should be marked in someway to distinguish between that and the value of the attribute that is in the dataset's metadata.

☑ This topic is SOLVED.

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

Discussion stats
  • 22 replies
  • 65557 views
  • 4 likes
  • 8 in conversation