Reputation: 113
I am using SAS EG, and in the following MWE, I am trying to update an existing character variable by concatenating some new characters to it.
data tmp;
input z $;
cards;
a
a
a
;
run;
data tmp2;
set tmp;
s ='x';
t = s || 'y';
s = s || 'y';
run;
data tmp3;
set tmp2;
a = 0;
do i=1 to 4;
a = a+i;
s = s || 'y';
end;
run;
So, this code will concatenate the two strings in t, but it does not update s. However, it does not have the same problem with numbers and as you cans see it can update at each cycle of the loop. So, can anyone tell me why it happens to character variables and how I can fix it.
Upvotes: 2
Views: 393
Reputation: 1804
That's because sas numbers (numeric) have a default length of 8 which is approximatly 15 digit floating point, but your s
is char with length=1.
You can confirm by running Proc SQL Describe Table
and checking the log:
Debug:
Proc sql; describe table tmp2; quit;
Log:
create table WORK.TMP2( bufsize=4096 )
(
z char(8),
s char(1),
t char(3)
);
Fix:
Length
statement to assign the lengthCats()
or Catx()
for concatenation instead of ||; this will remove leading and trailing spacesCode:
data tmp2;
set tmp;
length s $10 ;
s ='x';
t = cats(s,'y');
s = cats(s,'y');
run;
data tmp3;
set tmp2;
a = 0;
do i=1 to 4;
a = a+i;
s=cats(s,'y');
end;
run;
Upvotes: 2
Reputation: 8513
When a character variable is initialized in SAS it is assigned a length. In your example, the length assigned to variable s
was given a length of 1 as when you initialized it, you assigned a string of length 1 to it.
The solution is to use a length
statement to declare how long you would like the string to be beforehand:
length s $2;
Upvotes: 1