BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I am concatenating several datasets together. However, some of the character variables have different lengths and SAS determines the length of a variable based upon the first dataset in the SET statement. Therefore, data could be truncated.

What I want to do is set the length of all the character variables to 200 as a default. Any ideas how I can do this? I know I can use the LENGTH statement prior to the SET statement, but I’d have to name each variable and I don’t want to do that.

Thanks
5 REPLIES 5
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Liz,

You can use _CHARACTER_ in the LENGTH statement to get this:
[pre]
data o;
data i;
length _character_ $200;
run;
[/pre]
Sincerely,
SPR
Cynthia_sas
SAS Super FREQ
Hi:
LENGTH used in this fashion will give a WARNING:
[pre]
615 data newclass;
616 set sashelp.class;
617 length _character_ $200;
WARNING: Length of character variable Name has already been set.
Use the LENGTH statement as the very first statement in the DATA STEP to declare the
length of a character variable.
WARNING: Length of character variable Sex has already been set.
Use the LENGTH statement as the very first statement in the DATA STEP to declare the
length of a character variable.
618 run;
[/pre]

And, I do not believe that using _character_ in a LENGTH statement before the SET statement will work as desired...the names of the variables have to be used.

cynthia
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Cynthia,

"And, I do not believe that using _character_ in a LENGTH statement before the SET statement will work as desired...the names of the variables have to be used."

You are 100% right: it does not work and indicates some issue in SAS.

Sincerely,
SPR
ArtC
Rhodochrosite | Level 12
You will first need to determine the names of the character variables. There are several ways to do this. The following uses an SQL step to build a macro variable which is then used in the length statement.
[pre]/*proc sql ;*/
/*describe table dictionary.columns;*/
/*select libname, memname, name, type*/
/* from dictionary.columns*/
/* where (libname='SASHELP' and memname='CLASS'); */
/*quit;*/
proc sql noprint;
select name
into :charlist separated by ' '
from dictionary.columns
where (libname='SASHELP' and memname='CLASS' and type='char');
quit;
data long;
length &charlist $200;
set sashelp.class;
run;
proc contents data=long;
run;[/pre]
deleted_user
Not applicable
An alternative is to capture the lengths of all of the character variables via PROC CONTENTS, then determine the maximum length of each and write out a subroutine that you then call before your SET statement. That way you optimize your dataset size yet don't truncate any character variables.


Let's say you have three datasets (creatively named A, B, and C).
1) Run a contents, but keep just the name, type, and length, stripping off just the character variables.
2) Sort each contents dataset, renaming the length field to a unique name.
3) Merge the contents together, and determine the maximum length.
4) Write out a subroutine (in this case, "charlen.sas")
5) %include the subroutine (I love doing this. Your data helps you out.)


*** STEPS 1 & 2 ***;
proc contents data=A noprint out=Acon(keep=name type length where=(type=2));
proc sort data=Acon(keep=name length) out=Acon2(rename=(length=lengthA)); by name; run;

proc contents data=B noprint out=Bcon(keep=name type length where=(type=2));
proc sort data=Bcon(keep=name length) out=Bcon2(rename=(length=lengthB)); by name; run;

proc contents data=C noprint out=Ccon(keep=name type length where=(type=2));
proc sort data=Ccon(keep=name length) out=Ccon2(rename=(length=lengthC)); by name; run;
/* repeat as necessary for your datasets */

*** STEP 3 ***;
filename charlen "C:\Documents and Settings\mystuff\charlen.sas";
data _null_; merge Acon2 Bcon2 Ccon2; by name;
maxlen=max(lengthA,lengthB,lengthC);

*** STEP 4 ***;
file charlen recfm=v;
put "length " name "$" maxlen +(-1) ";";
run;

*** STEP 5 ***;
data ABC;
%include charlen;
set A B C;
run;

Mike

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
  • 5 replies
  • 1240 views
  • 0 likes
  • 4 in conversation