Example Programs‎ > ‎

Enhancing Output

This page shows the use of value and variable labels, as well as how to save output in HTML and LaTeX. The books offer much more coverage of factors than simply value labels. The practice data set is shown here. The programs and the data they use are also available for download here.


Value Labels

 R
 SAS SPSS Stata
mydata$workshop <-
  factor( mydata$workshop,
  levels = c(1, 2, 3, 4),
  labels = c("R", "SAS",
         "SPSS", "Stata") )




LIBNAME myLib 'C:\myRfolder';

PROC FORMAT;
  VALUES workshop_f
    1="R"
    2="SAS"
    3="SPSS"
    4="Stata";
  VALUES $gender_f
    "m"="Male"
    "f"="Female";
  VALUES agreement
    1='Strongly Disagree'
    2='Disagree'
    3='Neutral'
    4='Agree'
    5='Strongly Agree'.;
RUN;

DATA  myLib.mydata;
  SET myLib.mydata;
  FORMAT workshop workshop_f. gender gender_f.
  q1-q4 agreement.;
RUN;
CD 'C:\myRfolder'.
GET FILE='mydata.sav'.

VARIABLE LEVEL workshop
  (NOMINAL)
 /q1 TO q4 (SCALE).

VALUE LABELS 
  /workshop
  1 'R' 
  2 'SAS'
  3 'SPSS'
  4 'Stata'
  /q1 TO q4
  1 'Strongly Disagree'
  2 'Disagree'
  3 'Neutral'
  4 'Agree'
  5 'Strongly Agree'.

SAVE OUTfile = "mydata.sav".
use c:\myRfolder\mydata, clear
label var q1 "The instructor was well prepared"
label var q2 "The instructor communicated well"
label var q3 "The course materials were helpful"
label var q4 "Overall, I found this workshop useful"

* summarize the q variables
sum q*



Variable Labels

 R
 SAS SPSS Stata
# Filename: VarLabels.R

setwd("c:/myRfolder")
load(file = "mydata.RData")
options(width = 63)
mydata

# Using the Hmisc label attribute.
library("Hmisc")
label(mydata$q1) <-
  "The instructor was well prepared."
label(mydata$q2) <-
  "The instructor communicated well."
label(mydata$q3) <-
  "The course materials were helpful."
label(mydata$q4) <-
  "Overall, I found this workshop useful."

# Hmisc describe function uses the labels.
describe( mydata[ ,3:6] )

# Buit-in summary function
# ignores the labels.
summary( mydata[ ,3:6] )
* Filename: VarLabels.sas ;

LIBNAME myLib 'C:\myRfolder';

DATA  myLib.mydata;
  SET myLib.mydata ;
LABEL
Q1="The instructor was well prepared"
Q2="The instructor communicated well"
Q3="The course materials were helpful"
Q4="Overall, I found this workshop useful";
RUN;

PROC FREQ; TABLES q1-q4; RUN;
RUN;
* Filename: VarLabels.sps .

CD 'C:\myRfolder'.

GET FILE='mydata.sav'.
VARIABLE LABELS
  Q1 "The instructor was well prepared"
  Q2 "The instructor communicated well"
  Q3 "The course materials were helpful"
  Q4 "Overall, I found this workshop useful".

FREQUENCIES  VARIABLES=q1 q2 q3 q4.
SAVE OUTFILE='mydata.sav'.
* Stata Program for Variable Labels use mydata
* Filename: VarLabels.do

use c:\myRfolder\mydata, clear
label var q1 "The instructor was well prepared"
label var q2 "The instructor communicated well"
label var q3 "The course materials were helpful"
label var q4 "Overall, I found this workshop useful"

* summarize the q variables
sum q*



Writing HTML & LaTeX

 R
 SAS SPSS Stata
# Filename: FormattedOutput.R

options(width = 60)
setwd("c:/myRfolder")
load("mydata.RData")
attach(mydata)

library("xtable")

# Formatting a Data Frame

print(mydata)
myXtable <- xtable(mydata)
class(myXtable)

print(myXtable, type = "html")
print(myXtable, type = "latex")

# Formatting a Linear Model

mymodel <- lm( q4 ~ q1 + q2 + q3, data = mydata)
myXtable <- xtable(mymodel)

label(myXtable) <- c("xtableOutput")
caption(myXtable) <-
  c("Linear model results formatted by xtable")

print(myXtable, type = "html")
print(myXtable, type = "latex")
In SAS getting tabular results into your
word processor or Web page is as easy as setting the style when you install it and then saving the output directly in format you need. To get a subset of output, copy and paste works fine.
In SPSS, getting tabular results into your
word processor or Web page is as easy as setting the style when you install it and then saving the output directly in format you need. To get a subset of output copy and paste works fine.
use c:\myRfolder\mydata, list
regress q4  q1 q2 q3
outtex