I have import a file from access and there are several characters variables ,
by default, the length of variables are $255.
I have trying to use length statement to change them all together, but does not work
data want;
length _char_ $40.;
set have;
run;
Please help! Many thanks!
You cannot use variable lists to change the length. The variable lists only work on variables that are already defined, and once they are defined it is too late to change the length.
Update:
That is not 100% true. You can set the order with a retain without defining the length or type, but you still would need to list the names of the variables in the RETAIN statement.
So you could do:
data xx ;
retain name sex;
length _all_ $40;
set sashelp.class ;
run;
But it would be easier to just do:
data xx ;
length name sex $40.;
set sashelp.class ;
run;
Aside from the fact that the length should be $40 and not $40., yes, it won't work that way. One work-around might be:
proc sql noprint;
select name
into :names separated by ' '
from dictionary.columns
where libname eq 'SASHELP' and
memname eq 'CLASS' and
type eq 'char'
;
quit;
data want;
length &names $40;
set sashelp.class;
run;
this is wonderful! I can keep this macro for my further work
thanks a lot!!!
You cannot use variable lists to change the length. The variable lists only work on variables that are already defined, and once they are defined it is too late to change the length.
Update:
That is not 100% true. You can set the order with a retain without defining the length or type, but you still would need to list the names of the variables in the RETAIN statement.
So you could do:
data xx ;
retain name sex;
length _all_ $40;
set sashelp.class ;
run;
But it would be easier to just do:
data xx ;
length name sex $40.;
set sashelp.class ;
run;
yes, this will work for sure.
what I was trying is use _character_ to represent all character variables since I have more that 20 of them.
thanks anyway
You could try re-importing but using the DBMAX_TEXT option. SAS/ACCESS(R) 9.3 for Relational Databases: Reference, Second Edition
You could just leave the lengths as 255 and use the COMPRESS=yes option and SAS will not waste space storing the trailing spaces.
You could leave the length at 255 and attach a format.
data want ;
set have;
format _character_ $40.;
run;
Or perhaps you just need to REMOVE the format that SAS/Access insists on attaching when you import data.
data want ;
set have;
format _character_ ;
run;
I have tried both of them right after I proc import the file, but it dose not work:smileycry:
Changing the format will not change the length, but what difference does it make if the variable is defined longer than it needs to be? With the compress option on the extra spaces do not cause the stored dataset to be any bigger.
For example try this little experiment and see that storing the same information in a variable of length $255 and one of length $40 end up taking the same amount of disk space.
425 data one(keep=long compress=yes) two(keep=short compress=yes);
426 length long $255 short $40 ;
427 do i=1 to 5000 ;
428 short='This is less than 40 characters.';
429 long=short;
430 output;
431 end;
432 run;
NOTE: The data set WORK.ONE has 5000 observations and 1 variables.
NOTE: Compressing data set WORK.ONE decreased size by 75.00 percent.
Compressed is 5 pages; un-compressed would require 20 pages.
NOTE: The data set WORK.TWO has 5000 observations and 1 variables.
NOTE: Compressing data set WORK.TWO increased size by 25.00 percent.
Compressed is 5 pages; un-compressed would require 4 pages.
NOTE: DATA statement used (Total process time):
real time 0.05 seconds
cpu time 0.04 seconds
: I disagree. Whether compressed, or not, the shorter lengths lead to smaller file sizes.
I ran the following two sets of code and then looked at the resulting file sizes:
libname art "d:\art";
data art.long (keep=long) art.short (keep=short);
length long $255 short $40 ;
do i=1 to 5000 ;
short='This is less than 40 characters.';
long=short;
output;
end;
run;
data art.long_compressed (keep=long compress=yes) art.short_compressed (keep=short compress=yes);
length long $255 short $40 ;
do i=1 to 5000 ;
short='This is less than 40 characters.';
long=short;
output;
end;
run;
which resulted in:
filename/size
long/1265kb
short/201kb
long_compressed/253kb
short_compressed/237kb
As you can see, the short file is smaller in both cases.
But not by enough to get your knickers in a twist.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.