Lesson 13, Part 3: Listing files in a directory using R from a SAS PROC IML step¶
Check whether RLANG enables SAS to execute R language statements.¶
ods html close;
options nodate nonumber nonotes nosource;
proc options option=RLANG;
run;
The SAS System
SAS (r) Proprietary Software Release 9.4 TS1M7
RLANG Enables SAS to execute R language statements.
The SAS System
E3969440A681A2408885998500000009
ods html close;
options nodate nonumber nonotes nosource;
%put %sysget(R_HOME);
%put %sysget(SASROOT);
%put %sysget(TMP);
The SAS System
C:\Program Files\R\R-4.2.0
C:\Program Files\SASHome\SASFoundation\9.4
C:\Users\pmuhuri\AppData\Local\Temp\1
The SAS System
E3969440A681A2408885998500000011
ods html close;
options nodate nonumber nonotes nosource;
data _null_;
infile "!SASCFG\sasv9.cfg";
input;
put _infile_;
run;
proc iml;
submit / R;
dir <- "C:/Explore/SAS/Lesson1/SAS_Codes"
if (!dir.exists(dir)) {
stop("Directory does not exist: ", dir)
}
files <- list.files(dir,
pattern="\\.sas$",
full.names=TRUE,
ignore.case=TRUE)
print(files)
endsubmit;
quit;
[1] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex_create_folderpath.sas" [2] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex_get_SAS_License_Info.sas" [3] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex_percent_uniquote.sas" [4] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex1_create_folderpath.sas" [5] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex1_DSPS.sas" [6] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex10_Data_step_view_etc.sas" [7] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex12_Data_step_without_datalines.sas" [8] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex12b_Data_step_withAndwithout_datalines.sas" [9] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex13_Syntax_Errors.sas" [10] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex14_Pathname_Library.sas" [11] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex15_Week_1_List_of_SAS_Programs.sas" [12] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex18_LIBRARY_library.sas" [13] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex19_List_Files_Data_Step.sas" [14] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex2_Comments.sas" [15] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex2_Nested_Formats.sas" [16] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex20_License_Info.sas" [17] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex21_SAS_User_Interface.sas" [18] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex22_License_Info.sas" [19] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex23_Retrieve_SAS_Licence_Macro.sas" [20] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex23_RetrieveSASLicenseInfo.sas" [21] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex24_Contents_SAS_Python_R.sas" [22] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex24_Print_SAS_Python_R.sas" [23] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex25_DataStepWithDoLoop.sas" [24] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex3_DataProcSteps.sas" [25] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex4_DM_Clear.sas" [26] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex5_proc_printto.sas" [27] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex7_Referencing_SAS_Data_Sets.sas" [28] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex8_Contents_all_ods.sas" [29] "C:/Explore/SAS/Lesson1/SAS_Codes/Ex9_Contents_many_ways.sas" [30] "C:/Explore/SAS/Lesson1/SAS_Codes/ExampleAggregateManyWays.sas" [31] "C:/Explore/SAS/Lesson1/SAS_Codes/Extra1.sas" [32] "C:/Explore/SAS/Lesson1/SAS_Codes/HowToTakePatrialProcContents.sas" [33] "C:/Explore/SAS/Lesson1/SAS_Codes/Macro_read_data.sas" [34] "C:/Explore/SAS/Lesson1/SAS_Codes/Macro_read_data_fromSAS.sas" [35] "C:/Explore/SAS/Lesson1/SAS_Codes/Percent_unquote2.sas" [36] "C:/Explore/SAS/Lesson1/SAS_Codes/Percent_unquote3.sas" [37] "C:/Explore/SAS/Lesson1/SAS_Codes/PostgreSQL_read_pdf.sas" [38] "C:/Explore/SAS/Lesson1/SAS_Codes/PostgreSQL1.sas" [39] "C:/Explore/SAS/Lesson1/SAS_Codes/Proc_product_status_setinit.sas" [40] "C:/Explore/SAS/Lesson1/SAS_Codes/Temp4_rev6.sas"
PROC IML;
SUBMIT / R;
setwd ("C:/Explore/SAS/Lesson13/SAS_Codes")
Sys.glob("*.sas") # returns a sorted list of files
ENDSUBMIT;
QUIT;
[1] "Create_delete_folder.sas" [2] "Ex10_simulate.sas" [3] "Ex11_simulate.sas" [4] "Ex12_simulate.sas" [5] "Ex13_simulate.sas" [6] "Ex14_data_driven_simu.sas" [7] "Ex15_calling_R_from_SAS.sas" [8] "Ex16_calling_R_from_SAS.sas" [9] "Ex17_calling_R_from_SAS.sas" [10] "Ex18_collapse_multiple_cols.sas" [11] "Ex19_count_repeated_rows.sas" [12] "Ex1_Generate_Random_Numbers.sas" [13] "Ex20_macro_case_study.sas" [14] "Ex21_Load.sas" [15] "Ex22_R_List_of_Files.sas" [16] "Ex23_IML_R.sas" [17] "Ex24_simulate_regression.sas" [18] "Ex2_DoLoop_Mod_Func.sas" [19] "Ex3_call_symputx_dates.sas" [20] "Ex4_generate_dates_sashelp_class.sas" [21] "Ex5_generate_SSN_dates_other_vars.sas" [22] "Ex6_random_assignment.sas" [23] "Ex7_simualte_randomID.sas" [24] "Ex7a_simulate_state_name.sas" [25] "Ex8_simulate.sas" [26] "Ex9_simulate.sas" [27] "Simulate_calender_months.sas" [28] "Simulate_calender_months_2.sas" [29] "Simulate_data_for_surveymeans.sas" [30] "Simulate_state_data_arrays.sas"
The above syntax is fine for running R code within PROC IML, but Sys.glob("*.sas") only returns a vector of matching filenames in the current working directory; it does not automatically return anything to SAS output.
To make it useful in SAS, you need to either print the result in the R block or import it back into SAS. Consider the code below.
proc iml;
submit / R;
setwd("C:/Explore/SAS/Lesson13/SAS_Codes")
print(Sys.glob("*.sas"))
endsubmit;
quit;
[1] "Create_delete_folder.sas" [2] "Ex10_simulate.sas" [3] "Ex11_simulate.sas" [4] "Ex12_simulate.sas" [5] "Ex13_simulate.sas" [6] "Ex14_data_driven_simu.sas" [7] "Ex15_calling_R_from_SAS.sas" [8] "Ex16_calling_R_from_SAS.sas" [9] "Ex17_calling_R_from_SAS.sas" [10] "Ex18_collapse_multiple_cols.sas" [11] "Ex19_count_repeated_rows.sas" [12] "Ex1_Generate_Random_Numbers.sas" [13] "Ex20_macro_case_study.sas" [14] "Ex21_Load.sas" [15] "Ex22_R_List_of_Files.sas" [16] "Ex23_IML_R.sas" [17] "Ex24_simulate_regression.sas" [18] "Ex2_DoLoop_Mod_Func.sas" [19] "Ex3_call_symputx_dates.sas" [20] "Ex4_generate_dates_sashelp_class.sas" [21] "Ex5_generate_SSN_dates_other_vars.sas" [22] "Ex6_random_assignment.sas" [23] "Ex7_simualte_randomID.sas" [24] "Ex7a_simulate_state_name.sas" [25] "Ex8_simulate.sas" [26] "Ex9_simulate.sas" [27] "Simulate_calender_months.sas" [28] "Simulate_calender_months_2.sas" [29] "Simulate_data_for_surveymeans.sas" [30] "Simulate_state_data_arrays.sas"
Write to a CSV in R, and then read in SAS¶
proc iml;
submit / R;
setwd("C:/Explore/SAS/Lesson13/SAS_Codes")
files <- Sys.glob("*.sas")
write.csv(data.frame(filename = files),
"C:/Explore/SAS/Lesson13/SAS_Codes/sas_files.csv",
row.names = FALSE)
endsubmit;
quit;
proc import datafile="C:/Explore/SAS/Lesson13/SAS_Codes/sas_files.csv"
out=sas_files
dbms=csv
replace;
getnames=yes;
run;
You can transfer the R vector directly into SAS without writing an intermediate file by returning the R object from the SUBMIT block and using the SAS/IML RECEIVE facility.
Below are two working patterns — one using PROC IML's R submit and CREATE statements to build a SAS dataset directly, and a second that uses the LIST (print) + infile/pipe approach if your environment doesn't support direct RECEIVE.
*Ex46_Create_Newcars_SAS_R.sas;
data class_in_SAS;
set sashelp.class;
bmi = (weight / (height*height) ) * 703;
run;
proc print data=class_in_SAS (obs=3) noobs; run;
| Name | Sex | Age | Height | Weight | bmi |
|---|---|---|---|---|---|
| Alfred | M | 14 | 69.0 | 112.5 | 16.6115 |
| Alice | F | 13 | 56.5 | 84.0 | 18.4986 |
| Barbara | F | 13 | 65.3 | 98.0 | 16.1568 |
proc iml;
call ExportDataSetToR("work.class_in_SAS", "class_r"); * work.class_in_SAS created earlier;
submit / R;
names(class_r) <- tolower(names(class_r))
str(class_r)
setwd("C:/Explore/SAS/Lesson13/Lesson13Data")
save(class_r, file = 'class_r.Rdata')
endsubmit;
quit;
'data.frame': 19 obs. of 6 variables: $ name : chr "Alfred" "Alice" "Barbara" "Carol" ... $ sex : chr "M" "F" "F" "F" ... $ age : num 14 13 13 14 14 12 12 15 13 12 ... $ height: num 69 56.5 65.3 62.8 63.5 57.3 59.8 62.5 62.5 59 ... $ weight: num 112 84 98 102 102 ... $ bmi : num 16.6 18.5 16.2 18.3 17.9 ...
Manipulating data in R within PROC IML¶
- Use load() to load the R data into memory
- Use mutate() in R tidyverse-dplyr
PROC IML;
SUBMIT / R;
library("tidyverse")
setwd("C:/Explore/SAS/Lesson13/Lesson13Data")
load("class_r.Rdata")
class <- class_r
class$sex <- factor(class$sex, level=c('M', 'F'),
label=c('male', 'female')
)
class %>%
mutate(
bmi = (weight / (height*height) ) * 703
)
head(class)
ENDSUBMIT;
QUIT;
name sex age height weight bmi
1 Alfred male 14 69.0 112.5 16.61153
2 Alice female 13 56.5 84.0 18.49855
3 Barbara female 13 65.3 98.0 16.15679
4 Carol female 14 62.8 102.5 18.27090
5 Henry male 14 63.5 102.5 17.87030
6 James male 12 57.3 83.0 17.77150
7 Jane female 12 59.8 84.5 16.61153
8 Janet female 15 62.5 112.5 20.24640
9 Jeffrey male 13 62.5 84.0 15.11731
10 John male 12 59.0 99.5 20.09437
11 Joyce female 11 51.3 50.5 13.49000
12 Judy female 14 64.3 90.0 15.30298
13 Louise female 12 56.3 77.0 17.07770
14 Mary female 15 66.5 112.0 17.80451
15 Philip male 16 72.0 150.0 20.34144
16 Robert male 12 64.8 128.0 21.42966
17 Ronald male 15 67.0 133.0 20.82847
18 Thomas male 11 57.5 85.0 18.07335
19 William male 15 66.5 112.0 17.80451
name sex age height weight bmi
1 Alfred male 14 69.0 112.5 16.61153
2 Alice female 13 56.5 84.0 18.49855
3 Barbara female 13 65.3 98.0 16.15679
4 Carol female 14 62.8 102.5 18.27090
5 Henry male 14 63.5 102.5 17.87030
6 James male 12 57.3 83.0 17.77150