Discussion:
changing length and format of all character variables in SAS
(too old to reply)
Neerav Monga
2005-11-18 18:51:46 UTC
Permalink
Hi everyone,
I just received data in the form of a SPSS portable file which I can
read in using SAS. However, the problem is that SPSS codes all
character variables to be of length 200 AND formats them as $255.

When I go to view the data, I can't see the columns next to one another
b/c of this formatting by SPSS. Is there anyway to change the formats
and lengths of ALL character variables using a DATASTEP or SQL?

I'd be happy if the length was fixed say at 20, and format $20. as
well. I have around 60 variables that need to be coded like this.

Thanks for any advice you may have in advance,

Neerav
s***@yahoo.com
2005-11-18 19:08:51 UTC
Permalink
I will leave the length as 200, unless you know all positions after 20
are blanks.
The changing format in proc datasets will not physically change
anything but how it will show in log to print .
Look example below. Hope this helps.

data t1;
length a b $200;
a=repeat('a',199);
b=repeat('b',199);
format a b $255.;
run;

proc contents data=t1; run;
proc print data=t1; run;

proc datasets lib=work;
modify t1;
format a b $20.; *a b only;
*format _char_=$20.; *all char vars.
quit;

proc contents data=t1; run;

proc print data=t1; run;
proc print data=t1;
format a $80.;
run;
Neerav Monga
2005-11-18 19:31:48 UTC
Permalink
Can you use the same statement to modify the length of all character
vars in proc dataset?

i.e.

proc datasets lib=work;
modify t1;
length _char_ =$20;

I've looked at the data and yes there is no datafield that exceeds 20,
most are 10 or less.

Thanks.
data _null_;
2005-11-18 19:48:06 UTC
Permalink
Before you change the formats and possibly the length (a bit more
difficult) see if just disassociating the formats will help. Run this
example and note the difference. If you decide to change the length of
each variable you will need to measure first.

data work.char200;
length IamChar1-IamChar10 $200;
format IamChar1-IamChar10 $255.;
retain _character_ 'Character variable value';
run;
proc print; /* will generate truncation messages */
run;
proc datasets library=work nolist;
title 'Before';
contents data=char200;
run;
modify char200;
format _character_;
run;
title 'After';
contents data=char200;
run;
quit;
proc print; /* no truncation messages */
run;
Neerav Monga
2005-11-18 20:00:55 UTC
Permalink
Hi,
Good suggestion. I simply removed the length of the format, therefore I
don't have to worry about how long the length of the variable really
is.

As for changing the length of the variable, the following code works:


proc datasets lib=work;
modify test;
length _char_ $20; *all char vars.;
quit;

However, I actually used a macro I found on this newsgroup by Richard
Venezia that determines the max length of each variable and sets a
length statement accordingly to that variable. The macro is called
"resize", if anyone is looking for it.

Thanks for the help everyone, its good to have resources like this
instead of getting stuck on a simple problem on a Friday afternoon!

Cheers!

Neerav
Ian Whitlock
2005-11-18 19:43:00 UTC
Permalink
Neerav,
proc print data = ... ;
format _character_ $20. ;
run ;

If you really need to change the lengths then do it in a DATA step with

length var1 .... varn $20 ;
format _character_ $20. ;

BEFORE the SET statement.

Ian Whitlock
===============
Date: Fri, 18 Nov 2005 10:51:46 -0800
Reply-To: Neerav Monga <***@GMAIL.COM>
Sender: "SAS(r) Discussion"
From: Neerav Monga <***@GMAIL.COM>
Organization: http://groups.google.com
Subject: changing length and format of all character
variables in SAS
Comments: To: sas-l
Content-Type: text/plain; charset="iso-8859-1"
Hi everyone,
I just received data in the form of a SPSS portable file which I
can read in using SAS. However, the problem is that SPSS codes
all character variables to be of length 200 AND formats them as
$255.
When I go to view the data, I can't see the columns next to one
another b/c of this formatting by SPSS. Is there anyway to change
the formats and lengths of ALL character variables using a
DATASTEP or SQL?
I'd be happy if the length was fixed say at 20, and format $20.
as well. I have around 60 variables that need to be coded like
this.
Thanks for any advice you may have in advance,
Neerav
Neerav Monga
2005-11-18 20:29:46 UTC
Permalink
Thanks Ian,
I used proc datasets to do the same thing, but it would be even more
efficient to do it all in one datastep.

Neerav
Peter Crawford
2005-11-18 23:28:59 UTC
Permalink
You could use a view, most easily , a data step view, like:

data sasuser.shrink_widths /view= sasuser.shrink_widths ;
set &your_spss_data_transferred ;
format _character_ $11. ;
run;

Then viewing sasuser.shrink_widths will provide a quick/narrow view which
you can use as a base. Where you need to see wider columns, the viewtable
window options allow you to apply a wider format to any column.

Perhaps you prefer a more-permanent effect.
proc datasets nolist ;
modify &your_spss_data_transferred ;
format _character_ $11. ;
run;
quit;

Loading...