Repository navigation
Expand file tree
/
Copy pathSAS_dataset_example.R
More file actions
123 lines (109 loc) · 3.7 KB
/
Copy pathSAS_dataset_example.R
File metadata and controls
123 lines (109 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#Very short example of R code to access SAS datasets, and output the weighted median FBI
#Import libraries. Tidyverse as normal, haven for importing sas files and spatstat for weighted stats.
library(tidyverse)
library(haven)
library(spatstat)
library(data.table)
#Define input parameters
datayear <- 2022
sampyear <- 2022
#Function to apply name formats for farm types
apply_type_formats <- function(table_name) {
setkey(setDT(table_name), type)
table_name[setDT(fbs_type_tab), farmtype:=i.fbs_type_words]
return(table_name)
}
#Manually create a lookup table for farmtype names and numbering
fbs_type_numbers <- c(1:9)
fbs_type_words <- c("Cereals","General Cropping","Dairy","LFA Sheep","LFA Cattle","LFA Cattle and Sheep","Lowland Livestock","Mixed","All farm types")
fbs_type_tab <- data.frame(fbs_type_numbers, fbs_type_words)
#Input data folders
FBS_directory_path<- '//s0177a/sasdata1/ags/fas/'
agstemp_path <- '//s0177a/sasdata1/ags/census/agstemp/'
#Read in farm_account data
FBS_fa_data_file <- paste0("so_y", datayear,"_fa.sas7bdat")
FBS_fa_data <- tryCatch(
{
FBS_fa_data <- read_sas(FBS_fa_data_file)
},
error = function(e)
{
file.copy(paste0(FBS_directory_path, FBS_fa_data_file), getwd())
return(read_sas(FBS_fa_data_file))
}
)
#Basic cleaning of fa data - convert all column names to lower case and strip sas formatting
names(FBS_fa_data) <- tolower(names(FBS_fa_data))
for (x in colnames(FBS_fa_data)){
attr(FBS_fa_data[[deparse(as.name(x))]], "format.sas")=NULL
}
#Process fa dataset
FBS_data_process <- FBS_fa_data %>%
select(fa_id, fa_fbi, fa_aacv, fa_bslcv, type) %>%
mutate(pc = 100*fa_bslcv/fa_aacv)
#Read in weights file
FBS_weights_file <- paste0("new_weights.sas7bdat")
FBS_weights <- tryCatch(
{
FBS_weights <- read_sas(FBS_weights_file)
},
error = function(e)
{
file.copy(paste0(FBS_directory_path, FBS_weights_file), getwd())
return(read_sas(FBS_weights_file))
}
)
##Basic data cleaning - convert all column names to lower case and strip sas formatting
names(FBS_weights) <- tolower(names(FBS_weights))
for (x in colnames(FBS_weights)){
attr(FBS_weights[[deparse(as.name(x))]],"format.sas")=NULL
}
#Read in an agstemp file
FTEUnpaid_file <- paste0("FBS", datayear, "_FTEUnpaid_", sampyear-2000,".sas7bdat")
FTEUnpaid <- tryCatch(
{
FTEUnpaid <- read_sas(FTEUnpaid_file)
},
error = function(e)
{
file.copy(paste0(agstemp_path, FTEUnpaid_file), getwd())
return(read_sas(FTEUnpaid_file))
}
)
off_file <- paste0("FBS", datayear, "_offarmincome_", sampyear-2000,".sas7bdat")
off <- tryCatch(
{
off <- read_sas(off_file)
},
error = function(e)
{
file.copy(paste0(agstemp_path, off_file), getwd())
return(read_sas(off_file))
}
)
#Create a merged dataset with FBI and weights
MergedData <- FBS_data_process%>%
left_join(FBS_weights, by="fa_id")
MergedData <- MergedData %>%
mutate(ys_year=(fa_id%%10000)) %>%
filter(ys_year==sampyear)
##Apply wordy type format
MergedData <- apply_type_formats(MergedData)
#Output some values
mean(MergedData$fa_fbi)
weighted.mean(MergedData$fa_fbi,MergedData$fbswt)
weighted.median(MergedData$fa_fbi, MergedData$fbswt, type=1)
## Read in a dataset from opendata platform
#Need the opendatascot package (installation instructions: https://github.com/datasciencescotland/opendatascot)
library(opendatascot)
library(ggplot2)
#Can use the all datasets function to find the uri of the desired dataset
all_ods_datasets <- ods_all_datasets()
#Then use ods_dataset function to read in the dataset
CHD <- ods_dataset("cereal-and-oilseed-rape-harvest")
#Plot something
CHD_process <- CHD %>%
filter(agriculturalMeasure == "yield")
p <- ggplot(CHD_process, aes(x=refPeriod, y=value, color=crop))+
geom_line()
p