Lesson 13, Part 2: Manipulating data in R within PROC IML¶

  • Use load() to load the R data into memory
  • Use mutate() in R tidyverse-dplyr

Check whether you have permission to call R from the SAS system¶

In [36]:
options nocenter nodate nonumber nosource;
ods _ALL_ close;
ods listing close;
proc options option=RLANG;
run;
ods listing;
The SAS System

    SAS (r) Proprietary Software Release 9.4  TS1M7

 RLANG             Enables SAS to execute R language statements.

The SAS System

E3969440A681A2408885998500000038
In [1]:
PROC IML;
SUBMIT / R;
setwd ("C:\Explore\SAS\Lesson3\Lesson13data")
list.files(pattern="SAS$", 
           full.names = TRUE, 
           ignore.case = TRUE)
ENDSUBMIT;
QUIT;
SAS Output

The SAS System


 [1] "./Ex_readmission_lag_function.sas"                                                                                            

 [2] "./Ex_Sum_statement_cars_data.sas"                                                                                             

 [3] "./Ex1_Numeric_Character_Formats.sas"                                                                                          

 [4] "./Ex10_DO_Group.sas"                                                                                                          

 [5] "./Ex11_format_put_function.sas"                                                                                               

 [6] "./Ex12_IFC_IFN_Function.sas"                                                                                                  

 [7] "./Ex13_IFN_Fourth_Argument.sas"                                                                                               

 [8] "./Ex14_whichn_whichc.sas"                                                                                                     

 [9] "./Ex15_input_function.sas"                                                                                                    

[10] "./Ex16_put_function.sas"                                                                                                      

[11] "./Ex17_Temporary_Permanet_Catalogs.sas"                                                                                       

[12] "./Ex19_options_FMTSEARCH.sas"                                                                                                 

[13] "./Ex1B_Color_Coding.sas"                                                                                                      

[14] "./Ex2_Nested_Formats.sas"                                                                                                     

[15] "./Ex20_select_when.sas"                                                                                                       

[16] "./Ex21_create_variable_in_SQL.sas"                                                                                            

[17] "./Ex22_Retain_Sum_Statement.sas"                                                                                              

[18] "./Ex23_read_data_add_format.sas"                                                                                              

[19] "./Ex24_remove_labels_formats_informats.sas"                                                                                   

[20] "./Ex25_date_group.sas"                                                                                                        

[21] "./Ex26_delete_rows_all_vars_missing.sas"                                                                                      

[22] "./Ex27_Rounding_Function.sas"                                                                                                 

[23] "./Ex28_Format_Modify_Change_SDS.sas"                                                                                          

[24] "./Ex28_UserDefinedFormat.sas"                                                                                                 

[25] "./Ex29_Week_3_List_of_Files.sas"                                                                                              

[26] "./Ex3_picture_statement.sas"                                                                                                  

[27] "./Ex30_create_indicator_vars.sas"                                                                                             

[28] "./Ex31_multi_date_formats.sas"                                                                                                

[29] "./Ex32_Group_formats.sas"                                                                                                     

[30] "./Ex33_Order_formats.sas"                                                                                                     

[31] "./Ex34_Format_SAS_Python_R.sas"                                                                                               

[32] "./Ex35_DataStepFuncs.sas"                                                                                                     

[33] "./Ex36_Dopen_Dread.sas"                                                                                                       

[34] "./Ex4_Value_Cntlin_compared.sas"                                                                                              

[35] "./Ex5_print_catalog_fmtlib.sas"                                                                                               

[36] "./Ex6_Finding_Invalids.sas"                                                                                                   

[37] "./Ex8_Invalue_statement.sas"                                                                                                  

[38] "./Ex9_Create_vars_Different_Ways.sas"                                                                                         

In [1]:
PROC IML;
SUBMIT / R;
setwd ("C:/Users/pmuhuri/SASCourse/Week3/SAS_Codes")
Sys.glob("*.sas")  # returns a sorted list of files
ENDSUBMIT;
QUIT;
SAS Output

The SAS System


 [1] "Ex10_DO_Group.sas"                                                                                                            

 [2] "Ex11_format_put_function.sas"                                                                                                 

 [3] "Ex12_IFC_IFN_Function.sas"                                                                                                    

 [4] "Ex13_IFN_Fourth_Argument.sas"                                                                                                 

 [5] "Ex14_whichn_whichc.sas"                                                                                                       

 [6] "Ex15_input_function.sas"                                                                                                      

 [7] "Ex16_put_function.sas"                                                                                                        

 [8] "Ex17_Temporary_Permanet_Catalogs.sas"                                                                                         

 [9] "Ex19_options_FMTSEARCH.sas"                                                                                                   

[10] "Ex1B_Color_Coding.sas"                                                                                                        

[11] "Ex1_Numeric_Character_Formats.sas"                                                                                            

[12] "Ex20_select_when.sas"                                                                                                         

[13] "Ex21_create_variable_in_SQL.sas"                                                                                              

[14] "Ex22_Retain_Sum_Statement.sas"                                                                                                

[15] "Ex23_read_data_add_format.sas"                                                                                                

[16] "Ex24_remove_labels_formats_informats.sas"                                                                                     

[17] "Ex25_date_group.sas"                                                                                                          

[18] "Ex26_delete_rows_all_vars_missing.sas"                                                                                        

[19] "Ex27_Rounding_Function.sas"                                                                                                   

[20] "Ex28_Format_Modify_Change_SDS.sas"                                                                                            

[21] "Ex28_UserDefinedFormat.sas"                                                                                                   

[22] "Ex29_Week_3_List_of_Files.sas"                                                                                                

[23] "Ex2_Nested_Formats.sas"                                                                                                       

[24] "Ex30_create_indicator_vars.sas"                                                                                               

[25] "Ex31_multi_date_formats.sas"                                                                                                  

[26] "Ex32_Group_formats.sas"                                                                                                       

[27] "Ex33_Order_formats.sas"                                                                                                       

[28] "Ex34_Format_SAS_Python_R.sas"                                                                                                 

[29] "Ex35_DataStepFuncs.sas"                                                                                                       

[30] "Ex36_Dopen_Dread.sas"                                                                                                         

[31] "Ex3_picture_statement.sas"                                                                                                    

[32] "Ex4_Value_Cntlin_compared.sas"                                                                                                

[33] "Ex5_print_catalog_fmtlib.sas"                                                                                                 

[34] "Ex6_Finding_Invalids.sas"                                                                                                     

[35] "Ex8_Invalue_statement.sas"                                                                                                    

[36] "Ex9_Create_vars_Different_Ways.sas"                                                                                           

[37] "Ex_CreateVars.sas"                                                                                                            

[38] "Ex_Sum_statement_cars_data.sas"                                                                                               

[39] "Ex_readmission_lag_function.sas"                                                                                              

In [28]:
*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;
SAS Output

The SAS System

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
In [ ]:
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\Lesson3\Lesson3Data")
save(class_r, file = 'class_r.Rdata')
endsubmit;
quit;
In [ ]:
PROC IML;
SUBMIT / R;
library("tidyverse")
setwd("C:\Explore\SAS\Lesson3\Lesson3Data")
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;