Multiple Assignments or the Idiosyncrasies of R and SAS

May 8, 2008
By

(This article was first published on fernandohrosa.com.br - en » R, and kindly contributed to R-bloggers)

Working on top of someone else’s code at work a couple of weeks ago I stumbled upon a piece of SAS code that went like this:

 1 2 3   length check 3; ... check = Name = 'Netter'

Being raised in the land of open-source and having programmed in R since I was in college, that struck me at first as an implicit cast error, as check was previously declared integer. Or a misguided attempt to create an indicator variable. What surprised me even more was that not only there was no cast error as well as the code ran and gave a sensible result in the context of the program I was editing.

If you still did not spot the weirdness of the statement above, try running the exact same piece of code in R (parenthesis added on purpose to return the result of the assignment):

View Code RSPLUS
 1 2 3 4 5 6   R > ( check = Name = 'Netter' ) [1] "Netter" R > str(check) chr "Netter" R > Name [1] "Netter"

Now the explanation: due to the way assignments and comparison operators are defined across both languages, the same code in R will result in a Multiple Assignment, erasing the previous value of check and writing a character string over it, the same with Name, which will also be set to character.

In SAS, where the = operator does assignments as well as comparisons (though one could argue that you could use the ‘eq’ mnemonic instead) the same expression evaluates if the CHAR variable Name is equal to the constant ‘Netter’, and the result of this logical expression (0 or 1) is then attributed to the check (NUMERIC) variable.

Code snippet:

 1 2 3 4 5 6 7 8 9 10 11 12 13  data sample; input id nome \$; datalines; 1 Netter 2 Nelder 3 McCulough ; run;   data sample; set sample; chk = nome = 'Netter'; run;