# knitr::opts_chunk$set(echo = TRUE)
require(SASmarkdown)
## Loading required package: SASmarkdown
## sas, saslog, sashtml, and sashtmllog engines
## are now ready to use.
saspath <- "C:/Program Files/SASHome/SASFoundation/9.4/sas.exe"
sasopts <- "-nosplash -ls 75"
knitr::opts_chunk$set(engine='sashtml', engine.path=saspath,
engine.opts=sasopts, comment="")
2
3 /*print mean of a variable to log in SAS*/
4 proc sql noprint;
5 select std(age) format=best32.
6 into :age_mean
7 from sashelp.class;
8 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
9
10 %put Mean of age: &age_mean;
Mean of age: 1.49267215939689
11 %putlog Mean of age: &age_mean;
_
180
WARNING: Apparent invocation of macro PUTLOG not resolved.
ERROR 180-322: Statement is not valid or it is used out of proper order.
ERROR: Errors printed on page 1.
proc sql;
select name
into :vars separated by ' '
from dictionary.columns
where libname="SASHELP" and
memname="CLASS" and varnum=4;
;
quit;
%put &vars.;
Column Name
--------------------------------
Height
data do_to;
x=10;
y="yes";
do i=1 to 10;
x=x+1;
output;
end;
run;
proc print data=do_to;
run;
Obs x y i
1 11 yes 1
2 12 yes 2
3 13 yes 3
4 14 yes 4
5 15 yes 5
6 16 yes 6
7 17 yes 7
8 18 yes 8
9 19 yes 9
10 20 yes 10
use while or until
data loan;
balance=1000;
payment=0;
do while (balance>0);
balance=balance-100;
payment=payment+1;
output;
end;
run;
proc print data=loan;
run;
Obs balance payment
1 900 1
2 800 2
3 700 3
4 600 4
5 500 5
6 400 6
7 300 7
8 200 8
9 100 9
10 0 10
do loop within a macro
%macro run_calculation(amt, t, r);
data customer_value;
i=&r./10.;
do n=0 to &t.;
S=&amt.*((1+i)*n - 1)/i; /*Power operator */
output; /*output s*/
end;
file print;
putlog s @@;
put s @@;
run;
proc print data=customer_value;
run;
%mend;
%run_calculation(amt=100, t=10, r=7);
2285.7142857
Obs i n S
1 0.7 0 -142.86
2 0.7 1 100.00
3 0.7 2 342.86
4 0.7 3 585.71
5 0.7 4 828.57
6 0.7 5 1071.43
7 0.7 6 1314.29
8 0.7 7 1557.14
9 0.7 8 1800.00
10 0.7 9 2042.86
11 0.7 10 2285.71
proc means data=sashelp.class;
var _NUMERIC_; /*_CHARACTER_*/
output out=want mean= sum= max= /autoname;
run;
proc report data= want; /*not use output but dataset*/
Column _FREQ_;
Column Age_Mean;
Column Age_Sum;
Column Age_max;
Define _FREQ_ /"The total number" display;
Define Age_mean /"Mean of age" display;
Define Age_sum /"Sum of age" display;
Define Age_max /"Max of age" display;
Run;
The MEANS Procedure
Variable N Mean Std Dev Minimum Maximum
-------------------------------------------------------------------------
Age 19 13.3157895 1.4926722 11.0000000 16.0000000
Height 19 62.3368421 5.1270752 51.3000000 72.0000000
Weight 19 100.0263158 22.7739335 50.5000000 150.0000000
-------------------------------------------------------------------------
The
total Mean of Sum of Max of
number age age age
19 13.315789 253 16
2
3 %macro means(var_avg) ;
4
5 /*calculate means*/
6 proc means data=sashelp.class StackODSOutput n mean std min p5
6 ! p95 max nmiss;
7 var &var_avg;
8 class sex;
9 ods output summary=result2;
10 run;
11
12 /*append then output*/
13 data masterresult2; * combine results;
14 set masterresult2 result2;
15 run;
16
17 %mend means;
18
19 /*use macro to merge all descriptive stats */
20 data masterresult2 ;
21 set _null_;
22 run;
NOTE: The data set WORK.MASTERRESULT2 has 0 observations and 0 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
23
24 %let vars=
25 age
26 height
27 weight
28 ;
29
30 %macro model ;
31 %do i=1 %to %sysfunc(countw(&vars));
32
33 %let x=%scan(&vars,&i);
34 %means( &x )
35
36 %end;
37 %mend model;
38
39 %model;
NOTE: The data set WORK.RESULT2 has 2 observations and 12 variables.
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The PROCEDURE MEANS printed page 1.
NOTE: PROCEDURE MEANS used (Total process time):
real time 0.02 seconds
cpu time 0.03 seconds
NOTE: There were 0 observations read from the data set WORK.MASTERRESULT2.
NOTE: There were 2 observations read from the data set WORK.RESULT2.
NOTE: The data set WORK.MASTERRESULT2 has 2 observations and 12 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
NOTE: The data set WORK.RESULT2 has 2 observations and 12 variables.
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The PROCEDURE MEANS printed page 2.
NOTE: PROCEDURE MEANS used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
WARNING: Multiple lengths were specified for the variable Variable by
input data set(s). This can cause truncation of data.
NOTE: There were 2 observations read from the data set WORK.MASTERRESULT2.
NOTE: There were 2 observations read from the data set WORK.RESULT2.
NOTE: The data set WORK.MASTERRESULT2 has 4 observations and 12 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
NOTE: The data set WORK.RESULT2 has 2 observations and 12 variables.
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The PROCEDURE MEANS printed page 3.
NOTE: PROCEDURE MEANS used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
WARNING: Multiple lengths were specified for the variable Variable by
input data set(s). This can cause truncation of data.
NOTE: There were 4 observations read from the data set WORK.MASTERRESULT2.
NOTE: There were 2 observations read from the data set WORK.RESULT2.
NOTE: The data set WORK.MASTERRESULT2 has 6 observations and 12 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
40
41 proc print data= masterresult2;
42 run;
NOTE: There were 6 observations read from the data set WORK.MASTERRESULT2.
NOTE: The PROCEDURE PRINT printed page 4.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
The MEANS Procedure
Analysis Variable : Age
N
Sex Obs N Mean Std Dev Minimum 5th Pctl
--------------------------------------------------------------------------
F 9 9 13.222222 1.394433 11.000000 11.000000
M 10 10 13.400000 1.646545 11.000000 11.000000
--------------------------------------------------------------------------
Analysis Variable : Age
N N
Sex Obs 95th Pctl Maximum Miss
------------------------------------------------
F 9 15.000000 15.000000 0
M 10 16.000000 16.000000 0
------------------------------------------------
The MEANS Procedure
Analysis Variable : Height
N
Sex Obs N Mean Std Dev Minimum 5th Pctl
--------------------------------------------------------------------------
F 9 9 60.588889 5.018328 51.300000 51.300000
M 10 10 63.910000 4.937937 57.300000 57.300000
--------------------------------------------------------------------------
Analysis Variable : Height
N N
Sex Obs 95th Pctl Maximum Miss
------------------------------------------------
F 9 66.500000 66.500000 0
M 10 72.000000 72.000000 0
------------------------------------------------
The MEANS Procedure
Analysis Variable : Weight
N
Sex Obs N Mean Std Dev Minimum 5th Pctl
--------------------------------------------------------------------------
F 9 9 90.111111 19.383914 50.500000 50.500000
M 10 10 108.950000 22.727186 83.000000 83.000000
--------------------------------------------------------------------------
Analysis Variable : Weight
N N
Sex Obs 95th Pctl Maximum Miss
------------------------------------------------
F 9 112.500000 112.500000 0
M 10 150.000000 150.000000 0
------------------------------------------------
Obs Sex NObs _control_ Variable N Mean StdDev
1 F 9 Age 9 13.222222 1.394433
2 M 10 1 Age 10 13.400000 1.646545
3 F 9 Hei 9 60.588889 5.018328
4 M 10 1 Hei 10 63.910000 4.937937
5 F 9 Wei 9 90.111111 19.383914
6 M 10 1 Wei 10 108.950000 22.727186
Obs Min P5 P95 Max NMiss
1 11.000000 11.000000 15.000000 15.000000 0
2 11.000000 11.000000 16.000000 16.000000 0
3 51.300000 51.300000 66.500000 66.500000 0
4 57.300000 57.300000 72.000000 72.000000 0
5 50.500000 50.500000 112.500000 112.500000 0
6 83.000000 83.000000 150.000000 150.000000 0