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

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 workSmiley Sad

data want;

     length _char_ $40.;

     set have;

run;

Please help! Many thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

9 REPLIES 9
art297
Opal | Level 21

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;

LillianLee
Calcite | Level 5

this is wonderful!  I can keep this macro for my further work Smiley Happy

thanks a lot!!!

Tom
Super User Tom
Super User

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;

LillianLee
Calcite | Level 5

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 anywaySmiley Happy

Tom
Super User Tom
Super User

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;

LillianLee
Calcite | Level 5

I have tried both of them right after I proc import the file, but it dose not work:smileycry:

Tom
Super User Tom
Super User

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

art297
Opal | Level 21

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

Tom
Super User Tom
Super User

But not by enough to get your knickers in a twist.

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
  • 9 replies
  • 4100 views
  • 7 likes
  • 3 in conversation