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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 720 views
  • 1 like
  • 2 in conversation