Multiple Assignments or the Idiosyncrasies of R and SAS

[This article was first published on fernandohrosa.com.br - en » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

Related references:

Venables, W.N, Ripley, B. D. (2000) S Programming.
Slaughter, S.J., Delwiche, L. D. (2004) Little Sas Book.

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;

flattr this!

To leave a comment for the author, please follow the link and comment on their blog: fernandohrosa.com.br - en » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)