#Question 1 # a. Write a SAS program to create a temporary SAS data set called Vote. # Use the variable name State, Party and Age. Age should be stored as a numeric variable, # State and Party should be stored as character variables. # b. Include a procedure to list the observations in this data set. # c. Include a procedure to compute frequencies for Party. # d. Repeat problem above (a to c) using R programming library(readr) #to read csv file setwd("D:/Rstudio Project") Vote2 <- read.csv(file = 'D:/Rstudio Project/Sas Week 12/political.csv', header =FALSE , sep=',') colnames(Vote2) <- c("State" , "Party", "Age") #change column names View(Vote2) #list all observation print(Vote2) #list all observation #calculate frequency #Method 1 table(Vote2$Party) #Method 2 p <- as.data.frame(table(Vote2$Party)) names(p)[1] = "Party" View(p) summary(p$Party) #View summary # Question 2 # Using the Bicycle data set (SAS native dataset), list all the # observations for Road Bike that cost more than $2,500 or Hybrid # that cost more than $660. The variable Model contains the type of bike # and UnitCost contains the cost. #teachers way, using sa7bdat library library(sas7bdat) bicycles2 <- read.sas7bdat(file = 'D:/Rstudio Project/Sas Week 12/bicycles.sas7bdat') outpl <- subset(bicycles2, (Model=='Road Bike' & UnitCost > 2500 | Model=='Hybrid' & UnitCost > 660), select=Country:Manuf) print(outpl) #own way bicycles <- read_sas("bicycles.sas7bdat", + NULL) View(bicycles) library(sqldf) sqldf("select * from bicycles where Model = 'Road Bike' and UnitCost > 2500 or Model = 'Hybrid' and Unitcost >660") #get the result using sql RoadBike <- bicycles[bicycles$UnitCost > 2500 & bicycles$Model == "Road Bike" ,] #get the result for roadbike Hybrid <- bicycles[bicycles$UnitCost > 660 & bicycles$Model == "Hybrid" ,] #get the result for roadbike Mergedbike <- merge(Hybrid,RoadBike, all = TRUE) #merge both datsets together View(Mergedbike) #Question 3 # Create a temporary SAS data set called Bank using this data file. Use using formatted # input to read the data values. Include in this data set a variable called Interest # computed by multiplying Balance by Rate. List the contents of this data set using # PROC PRINT. library(readr) library(tidyr) bankdata <- read_table2("bankdata.txt", col_names = FALSE) View(bankdata) bankdata <- unite(bankdata, Name, X1, X2, sep = " ") bankdata$Rate <- 0 colnames(bankdata) <- c("Name" , "Acct", "Balance", "Rate") View(bankdata) bankdata$Interest <- bankdata$Balance * bankdata$Rate View(bankdata) #teachers solution bankR <- read.fwf('D:/Rstudio Project/Sas Week 12/bankdata.txt', width=c(15,5,6,4), col.names = c("Name" , "Acct", "Balance", "Rate")) bankR$interest = bankR$balance = bankR$rate print(bankR)