I was just trying to add a reference line to the normal qqplot but why it is so tedious to do so in SAS? I have to calculate the mean and standard deviation of the data. Then save them to a temporary dataset. Then use the data procedure to read this mean/std data and assign them to two macros. Finally, I can use the macros to set the mean and std of the reference normal line........I believe there is a better way...I need to find out.
While, it is so easy in R. Just type in qqnorm() and qqline() will give you a pretty attractive qqplot. (well, not as beautiful as SAS graphic, but since we have ggplot2........)
DATA class;
INPUT Score @@;
datalines;
56 78 84 73 90 44 76 87 92 75
85 67 90 84 74 64 73 78 69 56
87 73 100 54 81 78 69 64 73 65
;
ODS GRAPHICS ON;
proc means data = class;
output out = statdata mean(score) = mu stddev(score) = sigma;
data _null_;
set statdata;
call symput("mu", mu);
call symput("sigma",sigma);
proc univariate data = class;
HISTOGRAM Score/NORMAL;
QQPLOT Score /NORMAL(mu = &mu sigma = &sigma color = red);
run;
Showing posts with label SAS. Show all posts
Showing posts with label SAS. Show all posts
6/20/2011
6/17/2011
Time to compare SAS and R -- Merge Dataset
I have been learning SAS which is quite an interesting language different from most of the languages I used. (Well it is quite similar to SQL syntax).
Today I am thinking whether R can do similar data manipulation as easy as SAS. Of course, the answer is: YES, R can!. I am going to show a simple example to demonstrate this. I will post other stuff while on my way to SAS master!
The example is from "The Little SAS Book":
We want to compare the two datasets and find out which customers did not make a purchase during a certain period. The datasets look like this:
# ADDR.
101 Murphy's Sports 115 Main St.
102 Sun N Ski 2106 Newberry Ave.
103 Sports Outfitters 19 Cary Way
104 Cramer & Johnson 4106 Arlington Blvd.
105 Sports Savers 2708 Broadway
# Sales
102 562.01
104 254.98
104 1642.00
101 3497.56
102 385.30
In SAS, you can easily find out which customers did not make a purchase:
data CustomerData;
input CustomerNumber Address & $30.;
datalines;
101 Murphy's Sports 115 Main St.
102 Sun N Ski 2106 Newberry Ave.
103 Sports Outfitters 19 Cary Way
104 Cramer & Johnson 4106 Arlington Blvd.
105 Sports Savers 2708 Broadway
;
data OrdersData;
input CustomerNumber Total;
datalines;
102 562.01
104 254.98
104 1642.00
101 3497.56
102 385.30
;
proc sort data = OrdersData;
by CustomerNumber;
run;
data MergedData;
merge CustomerData OrdersData (in = ifin);
by CustomerNumber;
if ifin = 0;
run;
Variable "mergedData" gives the merged dataset (as merge....by...statements in SAS). "nonmatchData" shows the customers without purchase, as shown in the SAS output.
Today I am thinking whether R can do similar data manipulation as easy as SAS. Of course, the answer is: YES, R can!. I am going to show a simple example to demonstrate this. I will post other stuff while on my way to SAS master!
The example is from "The Little SAS Book":
We want to compare the two datasets and find out which customers did not make a purchase during a certain period. The datasets look like this:
# ADDR.
101 Murphy's Sports 115 Main St.
102 Sun N Ski 2106 Newberry Ave.
103 Sports Outfitters 19 Cary Way
104 Cramer & Johnson 4106 Arlington Blvd.
105 Sports Savers 2708 Broadway
# Sales
102 562.01
104 254.98
104 1642.00
101 3497.56
102 385.30
In SAS, you can easily find out which customers did not make a purchase:
data CustomerData;
input CustomerNumber Address & $30.;
datalines;
101 Murphy's Sports 115 Main St.
102 Sun N Ski 2106 Newberry Ave.
103 Sports Outfitters 19 Cary Way
104 Cramer & Johnson 4106 Arlington Blvd.
105 Sports Savers 2708 Broadway
;
data OrdersData;
input CustomerNumber Total;
datalines;
102 562.01
104 254.98
104 1642.00
101 3497.56
102 385.30
;
proc sort data = OrdersData;
by CustomerNumber;
run;
data MergedData;
merge CustomerData OrdersData (in = ifin);
by CustomerNumber;
if ifin = 0;
run;
| Obs | CustomerNumber | Address | Total |
|---|---|---|---|
| 1 | 103 | Sports Outfitters 19 Cary Way | . |
| 2 | 105 | Sports Savers 2708 Broadway | . |
# TODO: Add comment
#
# Author: evert
###############################################################################
customerData = c(101, 'Murphy\'s Sports 115 Main St.',
102, 'Sun N Ski 2106 Newberry Ave.',
103, 'Sports Outfitters 19 Cary Way',
104, 'Cramer & Johnson 4106 Arlington Blvd.',
105, 'Sports Savers 2708 Broadway')
customerData = matrix(customerData, ncol = 2, byrow = TRUE)
customerData = data.frame(customerData)
colnames(customerData) = c("CustomerNumber", "Address")
ordersData = c( 102, 562.01,
104, 254.98,
104, 1642.00,
101, 3497.56,
102, 385.30)
ordersData = matrix(ordersData, ncol = 2, byrow = TRUE)
ordersData = data.frame(ordersData)
colnames(ordersData) = c("CustomerNumber", "Sales")
mergedData = merge(customerData,ordersData)
is.match = match(customerData$CustomerNumber, ordersData$CustomerNumber, nomatch = 0)
nonmatchData = customerData[which(is.match == 0),]
nonmatchData["Sales"] = NA
Variable "mergedData" gives the merged dataset (as merge....by...statements in SAS). "nonmatchData" shows the customers without purchase, as shown in the SAS output.
5/15/2011
Learning SAS Day 1 & 2
SAS syntax is quite different from common languages, such as R, Matlab, C++, etc. It is much closer to SQL syntax. Basically, you are not that free to control every aspect of the process, but it saves your effort by predefining some very easy to use procedures.
I have been working on DATA procedure, proc print, proc contents, proc format and some functions. I still do not quite get insight of the architecture of this language but I think I will figure this out pretty soon.
I have been working on DATA procedure, proc print, proc contents, proc format and some functions. I still do not quite get insight of the architecture of this language but I think I will figure this out pretty soon.
Labels:
SAS
Subscribe to:
Posts (Atom)