contact@a2zlearners.com

1.4. Migrating from SAS to R: A Skill Conversion Guide

1.4.10. Managing Workspace and Files in SAS vs R

1. Listing and Removing Objects

Capability SAS R
List objects PROC DATASETS; ls()
Remove objects DROP= in data steps, or PROC DATASETS with DELETE rm(object_name) or rm(list = ls())

SAS Example: Listing and Deleting Datasets in a Library

/* List all datasets in the WORK library */
proc datasets library=work;
run;

/* Delete a dataset */
proc datasets library=work nolist;
  delete mydata;
quit;

Explanation (SAS):

  • PROC DATASETS lists or modifies datasets in a library
  • The DELETE option removes specified datasets
  • nolist suppresses the output of the dataset list

R Example: Listing and Removing Objects in Workspace

# Create some objects
x <- 1:5
y <- data.frame(a = 1:3, b = 4:6)
z <- "hello"

# List objects in workspace
ls()
#> [1] "x" "y" "z"

# Remove one object
rm(z)
ls()
#> [1] "x" "y"

# Remove all objects
rm(list = ls())
ls()
#> character(0)

Explanation (R):

  • ls() lists objects in the current environment
  • rm() removes specified objects
  • rm(list = ls()) removes all objects in the environment

Expected Output (Both Examples):

Command SAS Output / R Console Output
ls() x y z
rm(z) (no output)
ls() x y
rm(list = ls()) (no output)
ls() (empty)

2. Exploring Object Structure

Capability SAS R
View structure PROC CONTENTS str(), summary(), or glimpse()
See variable names VAR statement in procedures names()
See attributes ATTRIB statement or PROC CONTENTS attributes()

SAS Example: Viewing Dataset Structure and Contents

/* View structure of a dataset */
proc contents data=sashelp.class;
run;

/* View first few rows of a dataset */
proc print data=sashelp.class(obs=5);
run;

Explanation (SAS):

  • PROC CONTENTS displays metadata about the dataset, including variable names and types
  • PROC PRINT shows the data values, with the obs= option limiting the number of rows

R Example: Exploring Data Frame Structure and Summary

# Create a data frame
df <- data.frame(id = 1:3, score = c(90, 85, 88))

# View structure
str(df)
#> 'data.frame':	3 obs. of 2 variables:
#>  $ id   : int  1 2 3
#>  $ score: num  90 85 88

# See column names
names(df)
#> [1] "id" "score"

# See attributes
attributes(df)
#> $names
#> [1] "id" "score"
#> $class
#> [1] "data.frame"
#> $row.names
#> [1] 1 2 3

Explanation (R):

  • str() displays the structure of an R object, including data frames
  • names() returns the column names of a data frame
  • attributes() shows the attributes of an object, such as a data frame

Expected Output (Both Examples):

Command SAS Output / R Console Output
proc contents (metadata about dataset)
proc print (first 5 rows of dataset)
str(df) (structure of df)
names(df) id score
attributes(df) (attributes of df)

3. Setting and Using the Working Directory

Capability SAS R
Get working directory %PUT statement with SYSPROCESS macro variable getwd()
Set working directory LIBNAME statement or FILENAME statement setwd()

SAS Example: Setting and Displaying the Working Directory

/* Set the working directory */
libname mydata "C:/Users/gnana/Documents/R_Project";

/* Display the current working directory */
%put Current working directory: &syspath;

Explanation (SAS):

  • LIBNAME associates a library reference (mydata) with a physical folder
  • %PUT writes text to the SAS log, including macro variable values

R Example: Setting and Displaying the Working Directory

# Show current working directory
getwd()
#> [1] "C:/Users/gnana/Documents"

# Set a new working directory
setwd("C:/Users/gnana/Documents/R_Project")
getwd()
#> [1] "C:/Users/gnana/Documents/R_Project"

Explanation (R):

  • getwd() returns the current working directory
  • setwd() sets the working directory to the specified path

Expected Output (Both Examples):

Command SAS Output / R Console Output
getwd() "C:/Users/gnana/Documents"
setwd() (no output)
getwd() "C:/Users/gnana/Documents/R_Project"

4. Saving and Loading Workspace

Capability SAS R
Save workspace SAVE statement in DATA step save.image()
Load workspace RESTORE statement in DATA step load()

SAS Example: Saving and Restoring the Workspace

/* Save the current workspace */
data _null_;
  set sashelp.class;
  file "C:/Users/gnana/Documents/class_data.txt";
  put (_all_) (+0);
run;

/* Restore the workspace from the saved file */
data class_data;
  infile "C:/Users/gnana/Documents/class_data.txt";
  input;
run;

Explanation (SAS):

  • The FILE statement in a DATA step writes data to an external file
  • The INFILE statement reads data from an external file

R Example: Saving and Loading Workspace Image

# Save all objects to a file
save.image(file = "myWorkspace.RData")

# Remove all objects
rm(list = ls())

# Load workspace back
load("myWorkspace.RData")
ls()
#> [1] "df" "x" "y"

Explanation (R):

  • save.image() saves the current workspace to a file
  • load() loads the workspace from a file

Expected Output (Both Examples):

Command SAS Output / R Console Output
save.image() (no output)
rm(list = ls()) (no output)
load() (no output)
ls() df x y

5. Visualizing the Search Path

Capability SAS R
Show search path OPTIONS statement with SASHELP search()

SAS Example: Displaying the Current Search Path

/* Show the current search path */
options ls=80;
proc sql;
  select path
  from dictionary.extfiles
  where fileref='SASHELP';
quit;

Explanation (SAS):

  • The OPTIONS statement sets the line size for the output
  • PROC SQL queries the DICTIONARY.EXTFILES table to get the path of the SASHELP library

R Example: Displaying the Search Path

# Show current search path
search()
#> [1] ".GlobalEnv" "package:stats" "package:graphics" ...

Explanation (R):

  • search() shows the current search path, including attached packages and environments

Expected Output (Both Examples):

Command SAS Output / R Console Output
search() ".GlobalEnv" "package:stats" "package:graphics" ...

6. Attaching and Detaching Data Frames

Capability SAS R
Attach data frame DATA step with SET statement attach()
Detach data frame DROP statement in DATA step detach()

SAS Example: Attaching and Detaching a Data Frame

/* Create a data frame */
data mydata;
  input id name $;
  datalines;
1 John
2 Mary
3 Sue
;
run;

/* Attach the data frame */
data _null_;
  set mydata;
  /* Now you can access 'id' and 'name' directly */
  put id= name=;
run;

/* Detach when done */
data _null_;
  set mydata;
  drop id name;
run;

Explanation (SAS):

  • The SET statement in a DATA step reads data from a dataset
  • The DROP statement excludes variables from the output dataset

R Example: Attaching and Detaching a .RData File

# Suppose you have a file "myall.RData" containing several objects (e.g., q1, q2, q3, q4, mydata, etc.)

# Create a variable in your workspace
x <- c(1,2,3,4,5,6,7,8)

# Attach the .RData file (does not load objects into workspace, but makes them available on the search path)
attach("myall.RData")

# Check the search path
search()
#> [1] ".GlobalEnv" "file:myall.RData" "package:stats" ...

# Access a variable (e.g., q4) from the attached file and copy it into your workspace
q4 <- q4

# Now q4 is available in your workspace (position 1)
ls()
#> [1] "x" "q4"

# List objects in the attached file (position 2)
ls(2)
#> [1] "mystats" "gender" "mydata" "mylist" "mymatrix" "q1" "q2" "q3" "q4" "workshop"

# Detach the file when done
detach("file:myall.RData")

Explanation (R):

  • attach("myall.RData") makes all objects in the file available on the search path, but does not copy them into your workspace.
  • You can access objects by name as if they were in your workspace, but to make a permanent copy, assign them (e.g., q4 <- q4).
  • ls(2) lists the objects in the attached file (position 2 in the search path).
  • detach("file:myall.RData") removes the file from the search path.

Expected Output:

Command R Console Output
ls() "x" "q4"
ls(2) "mystats" "gender" ... "q4" "workshop"
search() ".GlobalEnv" "file:myall.RData" ...
detach("file:myall.RData") (no output)

7. Saving and Loading Command History

Capability SAS R
Save command history PROC PRINTTO with LOG option savehistory()
Load command history PROC PRINTTO with LOG option loadhistory()

SAS Example: Saving and Restoring the Log

/* Save the log to a file */
proc printto log="C:/Users/gnana/Documents/myLog.log";
run;

/* Restore the log */
proc printto;
run;

Explanation (SAS):

  • PROC PRINTTO directs the output to a specified file
  • Running PROC PRINTTO with no options restores the default destination

R Example: Saving and Loading Command History

# Save command history
savehistory("myHistory.Rhistory")

# Load command history
loadhistory("myHistory.Rhistory")

Explanation (R):

  • savehistory() saves the command history to a file
  • loadhistory() loads the command history from a file

Expected Output (Both Examples):

Command SAS Output / R Console Output
savehistory() (no output)
loadhistory() (no output)

**Overall Summary Table: R Workspace and File Management**

Task/Action R Code Example Description/Notes
List all objects ls() Shows all objects in the current workspace
Remove one object rm(x) Removes object x
Remove multiple objects rm(x, y) Removes objects x and y
Remove all objects rm(list = ls()) Clears the workspace
Show structure of object str(df) Compact structure of df
Show column names names(df) Lists column names of a data frame
Show attributes attributes(df) Lists all attributes of an object
Set working directory setwd("C:/path") Changes working directory
Get working directory getwd() Shows current working directory
Save workspace save.image("myWorkspace.RData") Saves all objects to file
Save selected objects save(x, y, file="myData.RData") Saves only specified objects
Load workspace load("myWorkspace.RData") Loads objects from file
Attach data frame attach(df) Makes columns accessible by name
Detach data frame detach(df) Removes data frame from search path
Attach .RData file attach("myall.RData") Makes objects in file available on search path
Detach .RData file detach("file:myall.RData") Removes attached file from search path
List objects in attached file ls(2) Lists objects in position 2 of search path
Show search path search() Lists environments/packages in search path
Save command history savehistory("myHistory.Rhistory") Saves command history
Load command history loadhistory("myHistory.Rhistory") Loads command history

Tip:
You can visualize your workspace in RStudio using the Environment pane, which lists all objects and their types.
In SAS, use the Explorer window or PROC DATASETS to see available datasets.


**Resource download links**

1.4.10.-Managing-Workspace-and-Files-in-SAS-vs-R.zip