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

If I have an existing character (e.g. AREA) variable that's only 10 char and then I try to assign it a longer variable value to it then the value gets truncated down to 10.  How can I expand this variable (e.g. AREA) to a length of 25 characters before I assign the longer value to it?  So I can avoid it getting truncated?

 

Any help would be appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The log you posted did not match the code you posted immediately above it. In the code you properly had the LENGTH statement before the SET statement, but in the LOG the two statements were in the opposite order.

 

You cannot change teh length of a character variable once it has been established, that is why you need to have the LENGTH statement before the SET statement.

 

Also your subject line mentions FORMAT length.  The WIDTH of a format is different than the LENGTH of the variable.  If your character variable already had a format attached to it with a shorter length it can make the values appear to be truncate.  So make sure to REMOVE any format that might be attached to the variable in the source dataset.  

DATA &SITE._HCR_SUMMARY;
  LENGTH AREA $ 21;
  SET &SITE._SUMS_;
  format AREA ;
  ...

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

You may have to use the LENGTH statement to define an appropriate length so that the compiler determines and creates a descriptor/metadata(data about data) portion to hold a variable value to the limit of number of bytes specified. Once the data structure is defined, you should be all sett 

ballardw
Super User

If the variable was created with length 10 then you need tell SAS in the step you are attempting to change the value before the data is read into a data step:

 

Data want;

    length Area $ 25;

   set have;

run;

 

Sets the data vector for the Area variable to 25 characters before the values are read from the Set statement. You can modify the values of Area after the set statement up to length of 25 characters.

 

buechler66
Barite | Level 11

Ok, I tried to expand it below to 21 char, but then I get the error in the log below.  And the longer value I'm trying to assign is still truncated.  I must still be doing something wrong?

 

DATA &SITE._HCR_SUMMARY;
LENGTH AREA $ 21;
SET &SITE._SUMS_;
IF AREA = '' AND HCRID = '' AND PAY_DATA_MONTH = . AND ACTIVITY_DESCRIPTION = '' THEN AREA = 'AREA SITE GRAND TOTAL'; 
IF AREA NE '' AND HCRID NE '' AND PAY_DATA_MONTH = . AND ACTIVITY_DESCRIPTION = '' THEN ACTIVITY_DESCRIPTION = 'HCR CONTRACT TOTAL'; 
IF AREA NE '' AND HCRID NE '' AND PAY_DATA_MONTH NE . AND ACTIVITY_DESCRIPTION = '' THEN ACTIVITY_DESCRIPTION = catx(' ', upcase(put(pay_data_month,monname.)), 'CONTRACT SUBTOTAL'); 
DROP _TYPE_;
RENAME _FREQ_=FREQ PAY_DATA_MONTH=PAY_DATA_MONTH RATE=RATE_PER_UNIT;
FORMAT PAY_AMOUNT DOLLAR15.2 RATE DOLLAR15.2 UNITS COMMA15.2 PAY_DATA_MONTH MONYY7.;
PAY_DATA_MONTH_CHR = PUT(PAY_DATA_MONTH,MONYY7.);
RUN;

51   /* UPDATE THE SUMMARY OUTPUT ADDING DESCRIPTIVE LABELS FOR THE TOTALED CATEGORIES */
52   DATA &SITE._HCR_SUMMARY;
53   SET &SITE._SUMS_;
54   LENGTH AREA $ 21;
WARNING: Length of character variable AREA 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.
Tom
Super User Tom
Super User

The log you posted did not match the code you posted immediately above it. In the code you properly had the LENGTH statement before the SET statement, but in the LOG the two statements were in the opposite order.

 

You cannot change teh length of a character variable once it has been established, that is why you need to have the LENGTH statement before the SET statement.

 

Also your subject line mentions FORMAT length.  The WIDTH of a format is different than the LENGTH of the variable.  If your character variable already had a format attached to it with a shorter length it can make the values appear to be truncate.  So make sure to REMOVE any format that might be attached to the variable in the source dataset.  

DATA &SITE._HCR_SUMMARY;
  LENGTH AREA $ 21;
  SET &SITE._SUMS_;
  format AREA ;
  ...
buechler66
Barite | Level 11
Awesome! Thanks so much for your help. I really appreciate your time.
LeonidBatkhan
Lapis Lazuli | Level 10

It is absolutely possible. Here is an example that I hope is self-explanatory:

 

data A;
   length VAR $10;
   AREA= '1234567890';
   put AREA=;
run;

data A (drop=TEMP);
   set A (rename=(AREA=TEMP));
   length AREA $25;
   AREA = TEMP; output;
   AREA = "It is absolutely possible"; output;
run;

You can also take a look at my blog post Modifying variable attributes in all datasets of a SAS library for a broader view of this problem and possible solutions.

Best regards,

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 996 views
  • 1 like
  • 5 in conversation