[R] Merging data in R compared to SAS

rmailbox at justemail.net rmailbox at justemail.net
Wed Aug 22 20:52:11 CEST 2012


Untested code below

----- Original message -----
From: ramoss <ramine.mossadegh at finra.org>
To: r-help at r-project.org
Subject: [R] Merging data in R  compared to SAS
Date: Wed, 22 Aug 2012 07:59:04 -0700 (PDT)

Hello,

I am a SAS user new to R.  What is the R equivalent to following SAS
statements:

1) data all;
    merge test1(in=a)
              test2(in=b)
;
by account_id;
if a;
run;

a <- transform ( a, inA = TRUE)
b <- transform ( b, inB = TRUE)

all <- subset (  merge ( a, b, by = "account_id"), subset = inA )
# The merge will produce NAs where there was no match. Recode them to simplify tests for step 3:
transform ( all, inA = ifelse ( is.na( inA ), FALSE, inA) ), inB = ifelse ( is.na ( inB ), FALSE, inB ) )


2) proc sort data=all nodupkey;
 by account_id;
 run;

# You do not need the sort in R
allUniqueAccount <- subset ( all, !duplicated ( account_id) ) # You are sure dropping these is ok without inspection?

3)  data all test1onnly test2only;
    merge test1(in=a)
               test2(in=b)
  by account_id;
  if a and b then output all;
  else if a and not b the output test1only;
 else if b and not a then output test2only;
run;

all_AandB <- subset ( all,  inA  &  inB )
test1only <- subset ( all,  inA  & !inB )
test2only <- subset ( all, !inA &   inB )

Thanks in advance



--
View this message in context: http://r.789695.n4.nabble.com/Merging-data-in-R-compared-to-SAS-tp4640991.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list