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?
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;
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;
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;
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.
Hi Art,
We are running SAS 9.2 on Windows server 2008.
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. ?
That is correct. The length statement I tried was:
length wingband $6;
When I changed that to:
format wingband $6.;
it worked for me.
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.
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.
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;
Since the data is being imported from a dBase file your explanation must be correct.
Thanks for your help.
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.
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
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?
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.