Hi all,
I'm having trouble trying to assign a value to a string variable while using the retain statement.
I can retain a numeric variable while still being able to assign a new value to it, so that the value is continously evolving while going through each observation, as shown in the below simplified example with the variable "num".
But when I try to do the same with a characteristic variable, the variable seems to have been frozen to its initial value, which in this case, is "Test:".
Data test; Length str $ 100; retain num 0; retain str "TEST:"; set Sashelp.buy; if AMOUNT < -1000 then do; num = num + 1; str = str || "T"; end; else str = str || "F"; run;
The final outcome I'm trying to get is something like: "Test:TFFTTTTTTTF", in the last observation. However this is what I get at the moment using the above code.
The length command in the code was to show that the issue is not caused by issue with length of the string variable.
Thanks!
The operator "||" does NOT pre-trim the contents of its arguments. So the statement
str = str || "T";
essentially constructs at string of 101 bytes length (100 for str, and 1 for "T"). It then truncates all characters beyond number 100. Consider using
str=trim(str) || "T";
or
str=cats(str,"T");
The operator "||" does NOT pre-trim the contents of its arguments. So the statement
str = str || "T";
essentially constructs at string of 101 bytes length (100 for str, and 1 for "T"). It then truncates all characters beyond number 100. Consider using
str=trim(str) || "T";
or
str=cats(str,"T");
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.