BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
WesBarris
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
SAS Super FREQ

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;
Check out my Jedi SAS Tricks for SAS Users

View solution in original post

22 REPLIES 22
art297
Opal | Level 21

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;

WesBarris
Obsidian | Level 7

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;

art297
Opal | Level 21

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.

WesBarris
Obsidian | Level 7

Hi Art,

We are running SAS 9.2 on Windows server 2008.

art297
Opal | Level 21

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. ?

WesBarris
Obsidian | Level 7

That is correct.  The length statement I tried was:

length wingband $6;

When I changed that to:

format wingband $6.;

it worked for me.

art297
Opal | Level 21

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.

DLing
Obsidian | Level 7

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.

SASJedi
SAS Super FREQ

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;
Check out my Jedi SAS Tricks for SAS Users
WesBarris
Obsidian | Level 7

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

Thanks for your help.

art297
Opal | Level 21

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.

Tom
Super User Tom
Super User

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

art297
Opal | Level 21

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?

Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 22 replies
  • 299174 views
  • 14 likes
  • 8 in conversation