BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
wetman
Fluorite | Level 6

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.

retaintestscreenshot.png

The length command in the code was to show that the issue is not caused by issue with length of the string variable.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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 hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

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 hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
wetman
Fluorite | Level 6
Thank you so much! I tried both solutions and they all work for me!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 779 views
  • 1 like
  • 2 in conversation