DATA Step, Macro, Functions and more

how to change several characters length in one data step?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

how to change several characters length in one data step?

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!


Accepted Solutions
Solution
‎03-01-2014 04:11 PM
Super User
Super User
Posts: 7,074

Re: how to change several characters length in one data step?

Posted in reply to LillianLee

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


All Replies
PROC Star
Posts: 7,489

Re: how to change several characters length in one data step?

Posted in reply to LillianLee

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;

Occasional Contributor
Posts: 10

Re: how to change several characters length in one data step?

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

thanks a lot!!!

Solution
‎03-01-2014 04:11 PM
Super User
Super User
Posts: 7,074

Re: how to change several characters length in one data step?

Posted in reply to LillianLee

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;

Occasional Contributor
Posts: 10

Re: how to change several characters length in one data step?

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

Super User
Super User
Posts: 7,074

Re: how to change several characters length in one data step?

Posted in reply to LillianLee

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;

Occasional Contributor
Posts: 10

Re: how to change several characters length in one data step?

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

Super User
Super User
Posts: 7,074

Re: how to change several characters length in one data step?

Posted in reply to LillianLee

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

PROC Star
Posts: 7,489

Re: how to change several characters length in one data step?

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

Super User
Super User
Posts: 7,074

Re: how to change several characters length in one data step?

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 977 views
  • 7 likes
  • 3 in conversation