The value got strangely truncated:
data _null_;
length all $8000;
all='a9cb8d8d-a06d-4d77-a80b-8d69fe0808e5,48df6303-01d4-4181-84de-661a2f7267a1,HLS_ADVERSE_EVENT,severity_cd,VARCHAR(100),severity::coding[*]::code,ZERO-TO-ONE,FALSE,TRUE,FALSE,TRUE,TRUE,TRUE,TRUE,null,null,sas,sas';
new=tranwrd(all,"ZERO-TO-ONE","ZERO-TO-MANY");
put _all_;
run;
---------------------------------These red characters were gone.
all=a9cb8d8d-a06d-4d77-a80b-8d69fe0808e5,48df6303-01d4-4181-84de-661a2f7267a1,HLS_ADVERSE_EVENT,seve
rity_cd,VARCHAR(100),severity::coding[*]::code,ZERO-TO-ONE,FALSE,TRUE,FALSE,TRUE,TRUE,TRUE,TRUE,null
,null,sas,sas
new=a9cb8d8d-a06d-4d77-a80b-8d69fe0808e5,48df6303-01d4-4181-84de-661a2f7267a1,HLS_ADVERSE_EVENT,seve
rity_cd,VARCHAR(100),severity::coding[*]::code,ZERO-TO-MANY,FALSE,TRUE,FALSE,TRUE,TRUE,TRUE,TRUE,nul
l,nu _ERROR_=0 _N_=1
I don't know why they are got truncated. Thanks
You defined a length for ALL but you did not define a length of NEW.
So SAS was force to GUESS how to define NEW by how you first used it. Since it was the result of a character function (tranwrd) it probably decided that it should be a character variable of length 200.
Just define the variable BEFORE you use it.
data _null_;
length all new $8000;
You defined a length for ALL but you did not define a length of NEW.
So SAS was force to GUESS how to define NEW by how you first used it. Since it was the result of a character function (tranwrd) it probably decided that it should be a character variable of length 200.
Just define the variable BEFORE you use it.
data _null_;
length all new $8000;
Two more questions on this thread:
1) I am curious why TRANWRD function does not base the resultant value length on the target string, i.e. the variable ALL in the example. Different with other string functions, TRANWRD must have one and only one target. Is it very natural for the function to return a new value with the equal length, if its length is not yet defined? With this assumption, the variable NEW was not defined originally in the DATA-step.
2) How can TRANWRD be used as operands of another function, such as COALESCEC function. I.e. in order to avoid the truncation issue, must the resultant value be given to a pre-defined variable first? Here is the program where the truncation was originally found:
proc sql;
select coalescec(tranwrd(o.all,'ONE','TWOO'),t.all) as newcode length=500
from onedata as o natural left join
twodata as t
;
quit;
Clearly, truncation in TRANWRD still happens without being affected by the good length of NEWCODE. It will work that a temporary variable temp_var is derived first based on tranwrd(o.all,'ONE','TWOO'), then coalescec(calculated temp_var, t.all). However, the coding is a little clumsy. Is there another way to make it better?
There is a limit to how much SAS can do to read your mind.
In general SAS will default to length of 8 if there is no information available to make a different guess.
In general SAS will default to length of 200 when the only extra information is that the variable is being assigned the result of a character function. 200 used to be the maximum length of a character variable and I suspect that when they changed it with the short lived version 7 release they did not want to break existing code by changing from 200 to the new 32,767 maximum length.
There are some special cases, like the SUBSTR() function where SAS is able to make a (potentially) better guess. But TRANWRD() is not one of them. What length should it use? You could not just use the length of the first argument since the result of replacing some of the substrings with other substrings could result in a much longer string than the original first argument had.
If you know what you want the maximum length to be then actual DEFINE the variable and don't let SAS guess.
If you know what you are changing will not increase the length then you could just add an extra assignment statement first so SAS will know to guess to make the new the variable the same length as the old one.
newcode = all;
newcode = tranwrd(all,'ONE','TWO') ;
And forget trying to make any sense of SQL code. You have to type so much extra junk to get SQL syntax correct just get used to typing the desired length also.
Right in the documentation of the TRANWRD function tells what is going on. Set the length of the variable prior to use if the value may exceed 200.
Length of Returned Variable
In a DATA step, if the TRANWRD function returns a value to a variable that has not previously been assigned a length, then that variable is given a length of 200 bytes.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.