BookmarkSubscribeRSS Feed
sandyzman1
Obsidian | Level 7

Hi All,

 

I am trying to run correlation analysis in SAS that was previously done in R. I need to subset the dataset before the analysis, and I am having trouble understanding the R code for subsetting the dataset. Could anyone please help me to interpret the code and suggest equivalent code in SAS?

 

I have a knowledge of Base SAS and very little knowledge of R. Also, I am not very good at DO loop. 

 

I need to reproduce the following result from R to SAS:

 

 

for(i in unique(data$Site)){
     sum1_alternate <- sum1 <- sum2 <- sum3_alternate <- 0
for(j in unique(data[data$Site==i,]$id)){
     if(unique(data[data$site==i & data$id==j,]$ind)==1){
ind1_alternate <- ifelse(j %in% temp$id, 1, 0) 
sum1_alternate<- sum1_alternate + ind1_alternate

if(ind1_alternate == 1){
     ind1 <- ifelse(min(temp[temp$id==j,]$age)<21, 1, 0)
     sum1 <- sum1+ind1

ind2 <- ifelse(min(temp[temp$id==j,]$age)>21, 1, 0) 
     sum2 <- sum2+ind2
}
}
i

f(unique(data[data$site==i & data$id==j,]$ind)==0){
sum3_alternate <- sum3_alternate+1
#sum_info <- summary(data[data$site==i & data$id==j,]$age)
}
}

 

sum1_alternate_site <- c(sum1_alternate_site, sum1_alternate)
sum1_site <- c(sum1_site, sum1)
sum2_site <- c(sum2_site, sum2)
sum3_alternate_site <- c(sum3_alternate_site, sum3_alternate)
#sum3_site <- c(sum3_site, sum3)
}

 

percent1 <- sum1_alternate_site/(sum1_alternate_site+sum3_alternate_site) 
percent2 <- sum1_site/(sum1_site+sum2_site+sum3_alternate_site) 

 

plot(site_mean$x, percent1, xlab="Mean Age", ylab="Percentage of surgery", main="mean age vs percentage of surgery")

abline(lm(percent1 ~ site_mean$x))

cor.test(percent1, site_mean$x)

 

plot(site_mean$x, percent2, xlab="Mean Age", ylab="Percentage of surgery", main="mean age vs percentage of surgery <21 year-old")

abline(lm(percent2 ~ site_mean$x))
cor.test(percent2, site_mean$x) 

 

Thanks.

 

4 REPLIES 4
PaigeMiller
Diamond | Level 26

I can't tell you what the R code is doing, but correlation analysis in SAS is very simple.

 

proc corr data=have nosimple;
    by group;
    var x1 x2 x3;
run;

The BY statement computes correlations in different subsets of the data, as indicated by the values of variable GROUP.

--
Paige Miller
Ksharp
Super User

Calling @Rick_SAS 

Rick_SAS
SAS Super FREQ

Instead of posting the R program, please tell us more about the analysis you are trying to perform and how you want to subset the data. It would also be useful if you can provide some sample data in the form of a SAS DATA step that we can all run.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1898 views
  • 0 likes
  • 5 in conversation