+91 8600984908
Admin@riskycall.com

SAS and R : Data Management

31 Dec 2016

SAS and R : Data Management

//
Comments0
  This article includes data structure and data manipulations.  
1.Name of variables and their types

SAS
proc contents data dataname;
run;

R
str(dataname)

2.Rename variables

SAS
data dataname2;
  set dataname1(rename = (old1 = new1));
run;

R
names(dataname)[names(dataname) = =“old1”] = ”new1”

3. Condition if else

SAS
data dataname;
 set dataname;
 if oldvar le 0 then newvar = -1;

 else if oldvar eq 0 then newvar eq 0;
 else newvar = 1;
 run;

R
dataname$newvar <- NA
if(oldvar < 0) {
newvar = -1

} else if (oldvar =0){
 newvar = 0
} else newvar = 1

4. Concatenate string variables

SAS
data dataname;
 set dataname;

 newvar = x1|| “_” ||x2;
 run;

R
newvar = paste(x1,”_”,x2, sep = “”)

5. Subsetting observations

SAS
data dataname2;
 set dataname (where = (x eq 1));
run;

R
dataname2 = subset(dataname, x ==1)

5. Keep unique values

SAS
proc sort data = dataname out= dataname2 nodupkey;
 by x1 x2;
run;

R
dataname2 = dataname[!duplicated(datname[,c(‘x1′,’x2’)]),]

5. Merge datasets

SAS
proc sort data= dataname1 by x1;
 run;
proc sort data= dataname2 by x1; run;

data dataname3;
 merge dataname1 dataname2; by x1;
run;

R
dataname3 = merge(dataname1, dataname2, by = “x1”, all = TRUE)

Leave a Reply