9 SASmarkdown
9.1 How to install sasmarkdown
Use an Engine
# knitr::opts_chunk$set(echo = TRUE)
require(SASmarkdown)
## Loading required package: SASmarkdown
## SAS found at C:/Program Files/SASHome/SASFoundation/9.4/sas.exe
## SAS engines are now ready to use.
<- "C:/Program Files/SASHome/SASFoundation/9.4/sas.exe"
saspath <- "-nosplash -ls 75"
sasopts ::opts_chunk$set(engine='sashtml', engine.path=saspath,
knitrengine.opts=sasopts, comment="")
9.2 Common statements
Generally, can not use r syntax in these chunks
Data can not be used in different chunks
9.2.1 read in (date) data using informat
/*import and export raw data using infile*/
data test;
/*infile "c:/document/data.text";*/
input name $ height weight;
length height 4;
cards
;
Daniel 173 150
;
run;
proc print;run;
Obs name height weight
1 Daniel 173 150
data have;
input char_date : $20. fmt : $20.;
/* set a new variable format*/
num_char=inputn(char_date,fmt);
format num_char date9. ;
cards;
12-08-2015 DDMMYY12.
12/8/2016 MMDDYY12.
05/25/2015 MMDDYY12.
;
run;
proc print;run;
Obs char_date fmt num_char
1 12-08-2015 DDMMYY12. 12AUG2015
2 12/8/2016 MMDDYY12. 08DEC2016
3 05/25/2015 MMDDYY12. 25MAY2015
data have;
input char_date MMDDYY10. ;
/* output format */
format char_date date9. ;
cards;
12-08-2015
12/8/2016
05/25/2015
;
run;
proc print;run;
Obs char_date
1 08DEC2015
2 08DEC2016
3 25MAY2015
data have;
/* set input and output format*/
input char_date MMDDYY10. ;
format char_date MMDDYY10. ;
cards;
12-08-2015
12/8/2016
05/25/2015
;
run;
proc print;run;
Obs char_date
1 12/08/2015
2 12/08/2016
3 05/25/2015
9.2.2 compute mean and freqency
proc means data=sashelp.class maxdec=5 ;
run;
proc freq data=sashelp.class ;
table sex;
run;
proc contents data=sashelp.class varnum ;
run;
The MEANS Procedure
Variable N Mean Std Dev Minimum Maximum
-------------------------------------------------------------------------
Age 19 13.31579 1.49267 11.00000 16.00000
Height 19 62.33684 5.12708 51.30000 72.00000
Weight 19 100.02632 22.77393 50.50000 150.00000
-------------------------------------------------------------------------
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
--------------------------------------------------------
F 9 47.37 9 47.37
M 10 52.63 19 100.00
The CONTENTS Procedure
Data Set Name SASHELP.CLASS Observations 19
Member Type DATA Variables 5
Engine V9 Indexes 0
Created 09/06/2017 21:55:32 Observation Length 40
Last Modified 09/06/2017 21:55:32 Deleted Observations 0
Protection Compressed NO
Data Set Type Sorted NO
Label Student Data
Data Representation WINDOWS_64
Encoding us-ascii ASCII (ANSI)
Engine/Host Dependent Information
Data Set Page Size 65536
Number of Data Set Pages 1
First Data Page 1
Max Obs per Page 1632
Obs in First Data Page 19
Number of Data Set Repairs 0
ExtendObsCounter YES
Filename C:\Program
Files\SASHome\SASFoundation\9.
4\core\sashelp\class.sas7bdat
Release Created 9.0401M5
Host Created X64_SR12R2
Owner Name BUILTIN\Administrators
File Size 128KB
File Size (bytes) 131072
Variables in Creation Order
# Variable Type Len
1 Name Char 8
2 Sex Char 1
3 Age Num 8
4 Height Num 8
5 Weight Num 8
9.2.3 sort a data set
proc sort data=sashelp.class Out= name ;
by name;
Run;
proc print data= name (obs=10) ;
run;
Obs Name Sex Age Height Weight
1 Alfred M 14 69.0 112.5
2 Alice F 13 56.5 84.0
3 Barbara F 13 65.3 98.0
4 Carol F 14 62.8 102.5
5 Henry M 14 63.5 102.5
6 James M 12 57.3 83.0
7 Jane F 12 59.8 84.5
8 Janet F 15 62.5 112.5
9 Jeffrey M 13 62.5 84.0
10 John M 12 59.0 99.5
9.2.4 transpose or reshape
wide format data to long format data
proc transpose data= sashelp.class out= Field;
by name;
Run;
proc print data= field (obs=10) ;
run;
Obs Name _NAME_ COL1
1 Alfred Age 14.0
2 Alfred Height 69.0
3 Alfred Weight 112.5
4 Alice Age 13.0
5 Alice Height 56.5
6 Alice Weight 84.0
7 Barbara Age 13.0
8 Barbara Height 65.3
9 Barbara Weight 98.0
10 Carol Age 14.0
long format data to wide format data
proc transpose data= sashelp.class out= Field;
by name;
Run;
proc transpose data=Field out=Field_wide ;
by name ;
id _name_;
var col1;
run;
proc print data= field_wide (obs=10) ;
run;
Obs Name _NAME_ Age Height Weight
1 Alfred COL1 14 69.0 112.5
2 Alice COL1 13 56.5 84.0
3 Barbara COL1 13 65.3 98.0
4 Carol COL1 14 62.8 102.5
5 Henry COL1 14 63.5 102.5
6 James COL1 12 57.3 83.0
7 Jane COL1 12 59.8 84.5
8 Janet COL1 15 62.5 112.5
9 Jeffrey COL1 13 62.5 84.0
10 John COL1 12 59.0 99.5
9.2.5 conditional statement
DATA ab;
set sashelp.class;
IF sex="F" then message='A is greater';
Else message='B is greater';
Run;
proc print data=ab (obs=10);
run;
Obs Name Sex Age Height Weight message
1 Alfred M 14 69.0 112.5 B is greater
2 Alice F 13 56.5 84.0 A is greater
3 Barbara F 13 65.3 98.0 A is greater
4 Carol F 14 62.8 102.5 A is greater
5 Henry M 14 63.5 102.5 B is greater
6 James M 12 57.3 83.0 B is greater
7 Jane F 12 59.8 84.5 A is greater
8 Janet F 15 62.5 112.5 A is greater
9 Jeffrey M 13 62.5 84.0 B is greater
10 John M 12 59.0 99.5 B is greater
/*if else syntax*/
Data class;
set sashelp.class;
if weight >=100 then weight_cat =3;
else if weight >=90 and weight <100 then weight_cat =2 ;
else weight_cat =1;
attrib weight_cat label = "weight category, 1=Inad, 2=Adeq, 3=Exces";
run;
proc print data=class (obs=3);
run;
weight_
Obs Name Sex Age Height Weight cat
1 Alfred M 14 69.0 112.5 3
2 Alice F 13 56.5 84.0 1
3 Barbara F 13 65.3 98.0 2
/*if index*/
data test;
set sashelp.class;
length gender $10 ;
if index(sex,"M")>0 THEN gender="male";
if index(sex,"F")>0 THEN gender="female";
RUN;
proc print data=test (obs=5);
run;
Obs Name Sex Age Height Weight gender
1 Alfred M 14 69.0 112.5 male
2 Alice F 13 56.5 84.0 female
3 Barbara F 13 65.3 98.0 female
4 Carol F 14 62.8 102.5 female
5 Henry M 14 63.5 102.5 male
9.2.6 using like operation to select rows contaning a pattern
/*using like operation to select rows contaning a pattern-matching text.*/
proc print data =sashelp.class;
where upcase(name) like '%A%';
run;
proc print data =sashelp.class;
where (name) like '%A%';
run;
Obs Name Sex Age Height Weight
1 Alfred M 14 69.0 112.5
2 Alice F 13 56.5 84.0
3 Barbara F 13 65.3 98.0
4 Carol F 14 62.8 102.5
6 James M 12 57.3 83.0
7 Jane F 12 59.8 84.5
8 Janet F 15 62.5 112.5
14 Mary F 15 66.5 112.0
17 Ronald M 15 67.0 133.0
18 Thomas M 11 57.5 85.0
19 William M 15 66.5 112.0
Obs Name Sex Age Height Weight
1 Alfred M 14 69.0 112.5
2 Alice F 13 56.5 84.0
9.2.7 change format of the variable
proc format;
value AGEnew
11 = '1: NEW'
12 = '2: NEW'
13 = '3: NEW'
14 = '4: NEW'
15 = '5: NEW'
16 = '6: NEW'
;
run;
DATA ab;
set sashelp.class;
Format AGE AGEnew.;
Run;
proc freq data=ab;
table AGE;
run;
The FREQ Procedure
Cumulative Cumulative
Age Frequency Percent Frequency Percent
-----------------------------------------------------------
1: NEW 2 10.53 2 10.53
2: NEW 5 26.32 7 36.84
3: NEW 3 15.79 10 52.63
4: NEW 4 21.05 14 73.68
5: NEW 4 21.05 18 94.74
6: NEW 1 5.26 19 100.00
9.2.8 basic operations
Data Mathdata;
A= 10.12345;
B=20;
C= mean (a,b);
D= Min(a,b);
E= Max(a,b);
F = log(a);
G= round(a,0.02);
H= floor(a );
Run;
proc print data=Mathdata;
run;
Data mathdata;
Set sashelp.Iris;
Sum = sum (of SepalLength, SepalWIDTH);
Diff = SepalLength- SepalWIDTH;
Mult = SepalLength* SepalWIDTH;
Div= SepalLength/ SepalWIDTH;
Run;
proc print data=mathdata (obs=10);
run;
Obs A B C D E F G H
1 10.1235 20 15.0617 10.1235 20 2.31485 10.12 10
Sepal Sepal Petal Petal
Obs Species Length Width Length Width Sum Diff Mult Div
1 Setosa 50 33 14 2 83 17 1650 1.51515
2 Setosa 46 34 14 3 80 12 1564 1.35294
3 Setosa 46 36 10 2 82 10 1656 1.27778
4 Setosa 51 33 17 5 84 18 1683 1.54545
5 Setosa 55 35 13 2 90 20 1925 1.57143
6 Setosa 48 31 16 2 79 17 1488 1.54839
7 Setosa 52 34 14 2 86 18 1768 1.52941
8 Setosa 49 36 14 1 85 13 1764 1.36111
9 Setosa 44 32 13 2 76 12 1408 1.37500
10 Setosa 50 35 16 6 85 15 1750 1.42857
9.2.9 rename variables
a variable
Data AB;
set sashelp.class; Rename AGE= AGENEW;
Run;
proc print data=AB;
run;
Obs Name Sex AGENEW Height Weight
1 Alfred M 14 69.0 112.5
2 Alice F 13 56.5 84.0
3 Barbara F 13 65.3 98.0
4 Carol F 14 62.8 102.5
5 Henry M 14 63.5 102.5
6 James M 12 57.3 83.0
7 Jane F 12 59.8 84.5
8 Janet F 15 62.5 112.5
9 Jeffrey M 13 62.5 84.0
10 John M 12 59.0 99.5
11 Joyce F 11 51.3 50.5
12 Judy F 14 64.3 90.0
13 Louise F 12 56.3 77.0
14 Mary F 15 66.5 112.0
15 Philip M 16 72.0 150.0
16 Robert M 12 64.8 128.0
17 Ronald M 15 67.0 133.0
18 Thomas M 11 57.5 85.0
19 William M 15 66.5 112.0
multiple variables
2 /*rename*/
3 data AAA;
WARNING: The Base SAS Software product with which DATASTEP is associated
will be expiring soon, and is currently in warning mode to
indicate this upcoming expiration. Please run PROC SETINIT to
obtain more information on your warning period.
4 set sashelp.class;
5 run;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.AAA has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.01 seconds
6
7 /*create a list*/
8 proc sql noprint;
9 select cats(name,'=',name,'_new')
10 into : lis
11 separated by ' '
12 from dictionary.columns
13 where libname = 'WORK' and memname = 'AAA';
14 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.01 seconds
cpu time 0.03 seconds
15 %put &lis;
Name=Name_new Sex=Sex_new Age=Age_new Height=Height_new Weight=Weight_new
16
17 proc datasets library = work nolist;
18 modify AAA;
19 rename &lis;
NOTE: Renaming variable Name to Name_new.
NOTE: Renaming variable Sex to Sex_new.
NOTE: Renaming variable Age to Age_new.
NOTE: Renaming variable Height to Height_new.
NOTE: Renaming variable Weight to Weight_new.
20 quit;
NOTE: MODIFY was successful for WORK.AAA.DATA.
NOTE: PROCEDURE DATASETS used (Total process time):
real time 0.04 seconds
cpu time 0.03 seconds
21
22 proc print data=aaa (obs=2);
23 run;
NOTE: There were 2 observations read from the data set WORK.AAA.
NOTE: The PROCEDURE PRINT printed page 1.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
24
25 /*function to deal with text*/
26 %scan(&lis,2);
NOTE: Line generated by the macro function "SCAN".
26 Sex=Sex_new
___
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
27 /*#scan variable*/
28 %substr(NAME, %length(NAME),1);
NOTE: Line generated by the macro function "SUBSTR".
28 E
_
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
29 /*#extract character*/
30 %substr(NAME, 2,1);
NOTE: Line generated by the macro function "SUBSTR".
30 A
_
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
31 %index(&lis,"Age=Age_new") ;
NOTE: Line generated by the macro function "INDEX".
31 0
_
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
32 /*#identify this exists */
ERROR: Errors printed on page 1.
Height_ Weight_
Obs Name_new Sex_new Age_new new new
1 Alfred M 14 69.0 112.5
2 Alice F 13 56.5 84.0
9.2.10 extract text from a character value
Data Mathdata;
Text = "Hello World";
Text1= substr(Text, 6, 2);
Run;
proc print data=mathdata ;
run;
Obs Text Text1
1 Hello World W
9.2.11 convert the character value into a numeric value and reverse
Data ABC;
set sashelp.class;
agenew= input (age, best.);
Run;
proc print data=abc ;
run;
Obs Name Sex Age Height Weight agenew
1 Alfred M 14 69.0 112.5 14
2 Alice F 13 56.5 84.0 13
3 Barbara F 13 65.3 98.0 13
4 Carol F 14 62.8 102.5 14
5 Henry M 14 63.5 102.5 14
6 James M 12 57.3 83.0 12
7 Jane F 12 59.8 84.5 12
8 Janet F 15 62.5 112.5 15
9 Jeffrey M 13 62.5 84.0 13
10 John M 12 59.0 99.5 12
11 Joyce F 11 51.3 50.5 11
12 Judy F 14 64.3 90.0 14
13 Louise F 12 56.3 77.0 12
14 Mary F 15 66.5 112.0 15
15 Philip M 16 72.0 150.0 16
16 Robert M 12 64.8 128.0 12
17 Ronald M 15 67.0 133.0 15
18 Thomas M 11 57.5 85.0 11
19 William M 15 66.5 112.0 15
Data ABC;
set sashelp.class;
agenew= put (age, best.);
Run;
proc contents data=abc ;
run;
The CONTENTS Procedure
Data Set Name WORK.ABC Observations 19
Member Type DATA Variables 6
Engine V9 Indexes 0
Created 03/08/2024 09:19:15 Observation Length 48
Last Modified 03/08/2024 09:19:15 Deleted Observations 0
Protection Compressed NO
Data Set Type Sorted NO
Label
Data Representation WINDOWS_64
Encoding wlatin1 Western (Windows)
Engine/Host Dependent Information
Data Set Page Size 65536
Number of Data Set Pages 1
First Data Page 1
Max Obs per Page 1361
Obs in First Data Page 19
Number of Data Set Repairs 0
ExtendObsCounter YES
Filename C:\Users\hed2\AppData\Local\Temp\SAS Temporary
Files\_TD8664_HDW02234493_\abc.sas7bdat
Release Created 9.0401M5
Host Created X64_10PRO
Owner Name NIH\hed2
File Size 128KB
File Size (bytes) 131072
Alphabetic List of Variables and Attributes
# Variable Type Len
3 Age Num 8
4 Height Num 8
1 Name Char 8
2 Sex Char 1
5 Weight Num 8
6 agenew Char 12
9.2.12 change the length of the variable
Data ABC;
set sashelp.class;
Length agenew $10.;
Label agenew=“New age”;
Run;
proc contents data=abc ;
run;
The CONTENTS Procedure
Data Set Name WORK.ABC Observations 19
Member Type DATA Variables 6
Engine V9 Indexes 0
Created 03/08/2024 09:19:16 Observation Length 48
Last Modified 03/08/2024 09:19:16 Deleted Observations 0
Protection Compressed NO
Data Set Type Sorted NO
Label
Data Representation WINDOWS_64
Encoding wlatin1 Western (Windows)
Engine/Host Dependent Information
Data Set Page Size 65536
Number of Data Set Pages 1
First Data Page 1
Max Obs per Page 1361
Obs in First Data Page 19
Number of Data Set Repairs 0
ExtendObsCounter YES
Filename C:\Users\hed2\AppData\Local\Temp\SAS Temporary
Files\_TD12728_HDW02234493_\abc.sas7bdat
Release Created 9.0401M5
Host Created X64_10PRO
Owner Name NIH\hed2
File Size 128KB
File Size (bytes) 131072
Alphabetic List of Variables and Attributes
# Variable Type Len Label
3 Age Num 8
4 Height Num 8
1 Name Char 8
2 Sex Char 1
5 Weight Num 8
6 agenew Char 10 âNew ageâ
9.2.13 create a report
proc report data=sashelp.class;
Column age;
Define age / display;
Run;
Age
14
13
13
14
14
12
12
15
13
12
11
14
12
15
16
12
15
11
15
9.2.14 create a random variable
data ab;
set sashelp.class;
num=rand("normal");
run;
proc print data=ab (obs=10);
run;
Obs Name Sex Age Height Weight num
1 Alfred M 14 69.0 112.5 1.27381
2 Alice F 13 56.5 84.0 -2.17950
3 Barbara F 13 65.3 98.0 -0.98711
4 Carol F 14 62.8 102.5 0.75090
5 Henry M 14 63.5 102.5 -0.85480
6 James M 12 57.3 83.0 -0.33265
7 Jane F 12 59.8 84.5 0.16315
8 Janet F 15 62.5 112.5 -0.34409
9 Jeffrey M 13 62.5 84.0 -0.24306
10 John M 12 59.0 99.5 -2.13833
9.2.15 combine two texts
Data Mathdata;
Text = "Hello";
Text1= "World";
Text2= text || " " ||text1;
Run;
proc print data=Mathdata ;
run;
Obs Text Text1 Text2
1 Hello World Hello World
9.2.16 compress spaces
Data Mathdata;
Text = "Hello World ";
Text1= trim(text);
Text2= compress(text);
Run;
proc print data=Mathdata ;
run;
Obs Text Text1 Text2
1 Hello World Hello World HelloWorld
9.2.17 identify the position of a specified text
Data Mathdata;
Text = "Hello World";
indextext= index(text, "or");
Run;
proc print data=Mathdata ;
run;
Obs Text indextext
1 Hello World 8
9.2.18 convert upcase, lower case and propcase
Data Mathdata;
Text = "Hello World";
upcase= upcase(text );
lowcase= lowcase(text );
propcase= propcase(text );
Run;
proc print data=Mathdata ;
run;
Obs Text upcase lowcase propcase
1 Hello World HELLO WORLD hello world Hello World
9.2.19 deduplication
/* Dedup, original data has 19 observations */
proc sort data = sashelp.class out = dedup
nodupkeys;
by height;
run;
proc print data= dedup;
run;
Obs Name Sex Age Height Weight
1 Joyce F 11 51.3 50.5
2 Louise F 12 56.3 77.0
3 Alice F 13 56.5 84.0
4 James M 12 57.3 83.0
5 Thomas M 11 57.5 85.0
6 John M 12 59.0 99.5
7 Jane F 12 59.8 84.5
8 Janet F 15 62.5 112.5
9 Carol F 14 62.8 102.5
10 Henry M 14 63.5 102.5
11 Judy F 14 64.3 90.0
12 Robert M 12 64.8 128.0
13 Barbara F 13 65.3 98.0
14 Mary F 15 66.5 112.0
15 Ronald M 15 67.0 133.0
16 Alfred M 14 69.0 112.5
17 Philip M 16 72.0 150.0
The second way
proc sort data=sashelp.class out=data_rank; by height ; run;
data outdata dropdata;
set data_rank;
by height ;
if last.height then output outdata;
else output dropdata;
run;
proc print data=dropdata;run;
Obs Name Sex Age Height Weight
1 Janet F 15 62.5 112.5
2 Mary F 15 66.5 112.0
9.2.20 select sub data set
data where;
set sashelp.class;
where sex ne "F";
/*if */
run;
proc print data= where;
run;
Obs Name Sex Age Height Weight
1 Alfred M 14 69.0 112.5
2 Henry M 14 63.5 102.5
3 James M 12 57.3 83.0
4 Jeffrey M 13 62.5 84.0
5 John M 12 59.0 99.5
6 Philip M 16 72.0 150.0
7 Robert M 12 64.8 128.0
8 Ronald M 15 67.0 133.0
9 Thomas M 11 57.5 85.0
10 William M 15 66.5 112.0
9.2.21 create macro using do loop statement
2 /*create a macro to calcualte descriptive stats */
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 ;
WARNING: The Base SAS Software product with which DATASTEP is associated
will be expiring soon, and is currently in warning mode to
indicate this upcoming expiration. Please run PROC SETINIT to
obtain more information on your warning period.
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.02 seconds
cpu time 0.01 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.07 seconds
cpu time 0.07 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.01 seconds
cpu time 0.01 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.02 seconds
cpu time 0.03 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.01 seconds
cpu time 0.01 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.02 seconds
cpu time 0.03 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.01 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
43
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
9.2.22 output intermediate tables
/*extract tables using ods trace*/
ods trace on;
proc freq data=sashelp.class ;
table sex;
run;
ods trace off;
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
--------------------------------------------------------
F 9 47.37 9 47.37
M 10 52.63 19 100.00
ods listing close;
ods trace on;
ods output ParameterEstimates= ParameterEstimates ;
proc glm data=sashelp.class;
model height=age;
run;
ods trace off;
ods listing;
proc print data=ParameterEstimates;
run;
Obs Dependent Parameter Estimate StdErr tValue Probt
1 Height Intercept 25.22388451 6.52168912 3.87 0.0012
2 Height Age 2.78713911 0.48688163 5.72 <.0001
9.2.23 creat a sequence number
creat a sequence number based on a variable by grouping
/* grouped*/
proc sort data=sashelp.class out=class ; by name; run;
data temp; set class;
by name;
count + 1;
if first.name then count = 1;
run;
proc print data=temp (obs=3);run;
proc sort data=class out=class2 ; by height; run;
data class3;
set class2;
by height;
retain count;
if first.height then count=1;
else count=count+1;
run;
proc print data=class3 (obs=3);run;
/*nogrouped*/
data new;
set class;
seqno = _n_;
run;
proc print data=new (obs=3);run;
Obs Name Sex Age Height Weight count
1 Alfred M 14 69.0 112.5 1
2 Alice F 13 56.5 84.0 1
3 Barbara F 13 65.3 98.0 1
Obs Name Sex Age Height Weight count
1 Joyce F 11 51.3 50.5 1
2 Louise F 12 56.3 77.0 1
3 Alice F 13 56.5 84.0 1
Obs Name Sex Age Height Weight seqno
1 Alfred M 14 69.0 112.5 1
2 Alice F 13 56.5 84.0 2
3 Barbara F 13 65.3 98.0 3
9.2.24 merge data sets
right, left join
2 data class2;
WARNING: The Base SAS Software product with which DATASTEP is associated
will be expiring soon, and is currently in warning mode to
indicate this upcoming expiration. Please run PROC SETINIT to
obtain more information on your warning period.
3 input name $ score;
4 datalines;
NOTE: The data set WORK.CLASS2 has 3 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.01 seconds
8 ;
9 run;
10
11 proc sql;
12 create table class_c as
13 select *
14 from sashelp.class as a
15 left join class2 as b
16 on a.name = b.name;
WARNING: Variable name already exists on file WORK.CLASS_C.
NOTE: Table WORK.CLASS_C created, with 19 rows and 6 columns.
17 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.03 seconds
cpu time 0.03 seconds
18
19 proc print data=class_c;
20 run;
NOTE: There were 19 observations read from the data set WORK.CLASS_C.
NOTE: The PROCEDURE PRINT printed page 1.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.04 seconds
cpu time 0.04 seconds
21
22 proc sql;
23 create table class_d as
24 select *
25 from class2 as a
26 right join sashelp.class as b
27 on a.name = b.name;
WARNING: Variable Name already exists on file WORK.CLASS_D.
NOTE: Table WORK.CLASS_D created, with 19 rows and 6 columns.
28 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.02 seconds
cpu time 0.03 seconds
29
30 proc print data=class_d;
31 run;
NOTE: There were 19 observations read from the data set WORK.CLASS_D.
NOTE: The PROCEDURE PRINT printed page 2.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
32
Obs Name Sex Age Height Weight score
1 Alfred M 14 69.0 112.5 85
2 Alice F 13 56.5 84.0 89
3 Barbara F 13 65.3 98.0 .
4 Carol F 14 62.8 102.5 .
5 Henry M 14 63.5 102.5 .
6 James M 12 57.3 83.0 .
7 Jane F 12 59.8 84.5 .
8 Janet F 15 62.5 112.5 .
9 Jeffrey M 13 62.5 84.0 .
10 John M 12 59.0 99.5 .
11 Joyce F 11 51.3 50.5 .
12 Judy F 14 64.3 90.0 .
13 Louise F 12 56.3 77.0 .
14 Mary F 15 66.5 112.0 .
15 Philip M 16 72.0 150.0 .
16 Robert M 12 64.8 128.0 .
17 Ronald M 15 67.0 133.0 .
18 Thomas M 11 57.5 85.0 .
19 William M 15 66.5 112.0 .
Obs name score Sex Age Height Weight
1 Alfred 85 M 14 69.0 112.5
2 Alice 89 F 13 56.5 84.0
3 . F 13 65.3 98.0
4 . F 14 62.8 102.5
5 . M 14 63.5 102.5
6 . M 12 57.3 83.0
7 . F 12 59.8 84.5
8 . F 15 62.5 112.5
9 . M 13 62.5 84.0
10 . M 12 59.0 99.5
11 . F 11 51.3 50.5
12 . F 14 64.3 90.0
13 . F 12 56.3 77.0
14 . F 15 66.5 112.0
15 . M 16 72.0 150.0
16 . M 12 64.8 128.0
17 . M 15 67.0 133.0
18 . M 11 57.5 85.0
19 . M 15 66.5 112.0
full join
2 data class2;
WARNING: The Base SAS Software product with which DATASTEP is associated
will be expiring soon, and is currently in warning mode to
indicate this upcoming expiration. Please run PROC SETINIT to
obtain more information on your warning period.
3 input name $ score;
4 datalines;
NOTE: The data set WORK.CLASS2 has 3 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.01 seconds
8 ;
9 run;
10
11 proc sql;
12 create table class_e as
13 select *
14 from sashelp.class as a
15 full join class2 as b
16 on a.name = b.name;
WARNING: Variable name already exists on file WORK.CLASS_E.
NOTE: Table WORK.CLASS_E created, with 20 rows and 6 columns.
17 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.03 seconds
cpu time 0.03 seconds
18
19 proc print data=class_e;
20 run;
NOTE: There were 20 observations read from the data set WORK.CLASS_E.
NOTE: The PROCEDURE PRINT printed page 1.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.04 seconds
cpu time 0.04 seconds
21
Obs Name Sex Age Height Weight score
1 Alfred M 14 69.0 112.5 85
2 Alice F 13 56.5 84.0 89
3 Barbara F 13 65.3 98.0 .
4 Carol F 14 62.8 102.5 .
5 . . . 99
6 Henry M 14 63.5 102.5 .
7 James M 12 57.3 83.0 .
8 Jane F 12 59.8 84.5 .
9 Janet F 15 62.5 112.5 .
10 Jeffrey M 13 62.5 84.0 .
11 John M 12 59.0 99.5 .
12 Joyce F 11 51.3 50.5 .
13 Judy F 14 64.3 90.0 .
14 Louise F 12 56.3 77.0 .
15 Mary F 15 66.5 112.0 .
16 Philip M 16 72.0 150.0 .
17 Robert M 12 64.8 128.0 .
18 Ronald M 15 67.0 133.0 .
19 Thomas M 11 57.5 85.0 .
20 William M 15 66.5 112.0 .
or using merge for appending
data class2;
input name $ score;
datalines;
Alfred 85
Alice 89
Daniel 99
;
run;
data class_f;
merge sashelp.class class2;
by name;
/*if A and B; */
run;
proc print data=class_f;
run;
Obs Name Sex Age Height Weight score
1 Alfred M 14 69.0 112.5 85
2 Alice F 13 56.5 84.0 89
3 Barbara F 13 65.3 98.0 .
4 Carol F 14 62.8 102.5 .
5 Daniel . . . 99
6 Henry M 14 63.5 102.5 .
7 James M 12 57.3 83.0 .
8 Jane F 12 59.8 84.5 .
9 Janet F 15 62.5 112.5 .
10 Jeffrey M 13 62.5 84.0 .
11 John M 12 59.0 99.5 .
12 Joyce F 11 51.3 50.5 .
13 Judy F 14 64.3 90.0 .
14 Louise F 12 56.3 77.0 .
15 Mary F 15 66.5 112.0 .
16 Philip M 16 72.0 150.0 .
17 Robert M 12 64.8 128.0 .
18 Ronald M 15 67.0 133.0 .
19 Thomas M 11 57.5 85.0 .
20 William M 15 66.5 112.0 .
inner join
2 data class2;
WARNING: The Base SAS Software product with which DATASTEP is associated
will be expiring soon, and is currently in warning mode to
indicate this upcoming expiration. Please run PROC SETINIT to
obtain more information on your warning period.
3 input name $ score;
4 datalines;
NOTE: The data set WORK.CLASS2 has 3 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.03 seconds
8 ;
9 run;
10
11 proc sql;
12 create table class_e as
13 select *
14 from sashelp.class as a
15 inner join class2 as b
16 on a.name = b.name;
WARNING: Variable name already exists on file WORK.CLASS_E.
NOTE: Table WORK.CLASS_E created, with 2 rows and 6 columns.
17 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.04 seconds
cpu time 0.03 seconds
18
19 proc print data=class_e;
20 run;
NOTE: There were 2 observations read from the data set WORK.CLASS_E.
NOTE: The PROCEDURE PRINT printed page 1.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.05 seconds
cpu time 0.04 seconds
21
Obs Name Sex Age Height Weight score
1 Alfred M 14 69.0 112.5 85
2 Alice F 13 56.5 84.0 89
minus join
2 data class2;
WARNING: The Base SAS Software product with which DATASTEP is associated
will be expiring soon, and is currently in warning mode to
indicate this upcoming expiration. Please run PROC SETINIT to
obtain more information on your warning period.
3 input name $ score;
4 datalines;
NOTE: The data set WORK.CLASS2 has 3 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.03 seconds
8 ;
9 run;
10
11 proc sql;
12 create table class_e as
13 select *
14 from sashelp.class as a
15 left join class2 as b
16 on a.name = b.name
17 where b.name is NULL;
WARNING: Variable name already exists on file WORK.CLASS_E.
NOTE: Table WORK.CLASS_E created, with 17 rows and 6 columns.
18 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.03 seconds
cpu time 0.03 seconds
19
20 proc print data=class_e;
21 run;
NOTE: There were 17 observations read from the data set WORK.CLASS_E.
NOTE: The PROCEDURE PRINT printed page 1.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.05 seconds
cpu time 0.06 seconds
22
23 /*proc sql ;*/
24 /*create table arm as*/
25 /*select * from arm*/
26 /*where subject_id not in (select subject_id from ustwin);*/
27 /*quit;*/
Obs Name Sex Age Height Weight score
1 Barbara F 13 65.3 98.0 .
2 Carol F 14 62.8 102.5 .
3 Henry M 14 63.5 102.5 .
4 James M 12 57.3 83.0 .
5 Jane F 12 59.8 84.5 .
6 Janet F 15 62.5 112.5 .
7 Jeffrey M 13 62.5 84.0 .
8 John M 12 59.0 99.5 .
9 Joyce F 11 51.3 50.5 .
10 Judy F 14 64.3 90.0 .
11 Louise F 12 56.3 77.0 .
12 Mary F 15 66.5 112.0 .
13 Philip M 16 72.0 150.0 .
14 Robert M 12 64.8 128.0 .
15 Ronald M 15 67.0 133.0 .
16 Thomas M 11 57.5 85.0 .
17 William M 15 66.5 112.0 .
9.2.25 create table 1
*** Load utility macros;
%include "C:\Users\hed2\Downloads\mybook2\mybook2\sasmacro\create_table1.sas";
*** Specify input and output data sets, and the column variable.;
%let INPUT_DATA = sashelp.class;
%let OUTPUT_DATA = Table1 ;
%let COLVAR = sex;
/*chort*/
*** %AddText(text=Height);
*** %CategoricalRowVar2(rowVar=);
%ContinuousRowVar2(rowVar=height );
*** %AddText(text=);
%ContinuousRowVar2(rowVar=weight );
%ContinuousRowVar2(rowVar=Age );
proc print data= table1;
run;
/* Export Table 1 as a CSV file*/
proc export data=Table1 replace label
outfile="C:\Users\hed2\Downloads\mybook2\mybook2\sasmacro\table1.csv"
dbms=csv;
run;
Obs Variable freqpct_meansd
1 Height : Mean (SD) [N] 62.3 (5.1) [19]
2 Weight : Mean (SD) [N] 100.0 (22.8) [19]
3 Age : Mean (SD) [N] 13.3 (1.5) [19]
freqpct_meansd_
Obs g1 freqpct_meansd_g2 Prob
1 60.6 (5.0) [9] 63.9 (4.9) [10] 0.1943
2 90.1 (19.4) [9] 109.0 (22.7) [10] 0.1476
3 13.2 (1.4) [9] 13.4 (1.6) [10] 0.8695
9.2.26 create table 1 without statistical test
*** Load utility macros;
%include "C:\Users\hed2\Downloads\mybook2\mybook2\sasmacro/Utility Macros_updated_NG.sas";
/*
By counts
%let yourdata = sashelp.class;
%let output_data= By_sex;
%let formatsfolder= ;
%let yourfolder = ;
%let varlist_cat = ;
%let varlist_cont = age heigt weight;
%let output_order = height weight age;
%let decimal_max =3;
%let group_by = sex;
%let group_by_missing=0;
%Table_summary;
*/
9.2.27 perform a regression
proc glm data= sashelp.class;
model height=age;
Run;
The GLM Procedure
Number of Observations Read 19
Number of Observations Used 19
The GLM Procedure
Dependent Variable: Height
Sum of
Source DF Squares Mean Square F Value Pr > F
Model 1 311.5434756 311.5434756 32.77 <.0001
Error 17 161.6207349 9.5071021
Corrected Total 18 473.1642105
R-Square Coeff Var Root MSE Height Mean
0.658426 4.946287 3.083359 62.33684
Source DF Type I SS Mean Square F Value Pr > F
Age 1 311.5434756 311.5434756 32.77 <.0001
Source DF Type III SS Mean Square F Value Pr > F
Age 1 311.5434756 311.5434756 32.77 <.0001
Standard
Parameter Estimate Error t Value Pr > |t|
Intercept 25.22388451 6.52168912 3.87 0.0012
Age 2.78713911 0.48688163 5.72 <.0001
9.3 Using macro
9.3.1 Create a macro variable
2
3 /*print mean of a variable to log in SAS*/
WARNING: The Base SAS Software product with which SQL is associated will
be expiring soon, and is currently in warning mode to indicate
this upcoming expiration. Please run PROC SETINIT to obtain more
information on your warning period.
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.02 seconds
cpu time 0.03 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.
9.3.2 Select variables names
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
9.3.3 Use iterative DO TO syntax to iterate a Specific Number
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
9.3.4 PROC REPORT allows more flexibility in displaying the data
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
9.3.5 Create a macro to calculate descriptive stats and summarize tabels
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 ;
WARNING: The Base SAS Software product with which DATASTEP is associated
will be expiring soon, and is currently in warning mode to
indicate this upcoming expiration. Please run PROC SETINIT to
obtain more information on your warning period.
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.02 seconds
cpu time 0.03 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.08 seconds
cpu time 0.09 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.01 seconds
cpu time 0.01 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.02 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.01 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.02 seconds
cpu time 0.03 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.01 seconds
cpu time 0.01 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.01 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
9.4 Use SAS format
9.4.1 Creating labels for different values
PROC FORMAT;
VALUE LIKERT7_A
1,2,3 = "Disagree"
4 = "Neither Agree nor Disagree"
5,6,7 = "Agree"
RUN;
PROC FORMAT;
VALUE INCOME
LOW -< 20000 = "Low"
20000 -< 60000 = "Middle"
60000 - HIGH = "High";
RUN;
PROC FORMAT;
VALUE RACE
1 = "White"
2 = "Black"
OTHER = "Other";
RUN;
/*CREATING LABELS FOR CHARACTER VARIABLES*/
/*assigning labels to variable values*/
PROC FORMAT;
VALUE $GENDERLABEL
"M" = "Male"
"F" = "Female";
RUN;
/*Use defined format*/
DATA sample;
SET sashelp.class;
FORMAT sex GENDERLABEL. ;
RUN;
proc freq data=sample;
table sex;
run;
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
-----------------------------------------------------------
Female 9 47.37 9 47.37
Male 10 52.63 19 100.00
9.4.2 Storing farmats to a library- way 1
LIBNAME format "C:\Users\hed2\Downloads\code-storage\code\format";
PROC FORMAT LIBRARY=format ;
VALUE $GENDER
"M" = "Male_new"
"F" = "Female_new";
RUN;
/*Use stored defined format way 1 */
OPTIONS FMTSEARCH=(format);
DATA sample;
SET sashelp.class;
FORMAT sex GENDER. ;
RUN;
proc freq data=sample;
table sex;
run;
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
---------------------------------------------------------------
Female_new 9 47.37 9 47.37
Male_new 10 52.63 19 100.00
9.4.3 Use stored defined format- way 2
%INCLUDE 'C:\Users\hed2\Downloads\code-storage\code\format\format_test.sas';
DATA sample;
SET sashelp.class;
FORMAT sex GENDERsex. ;
RUN;
proc freq data=sample;
table sex;
run;
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
----------------------------------------------------------------
Female_new2 9 47.37 9 47.37
Male_new2 10 52.63 19 100.00
9.4.4 Use stored defined format- way 3
Add formats to existing formatlib; seperate creating labels, useing format and labeling variables.
LIBNAME format "C:\Users\hed2\Downloads\code-storage\code\format";
PROC FORMAT LIBRARY=format ;
VALUE $GENDER
"M" = "Male_new"
"F" = "Female_new";
RUN;
/*add formats to existing formatlib*/
PROC FORMAT LIBRARY=format ;
VALUE $gendersextwo
"M" = "Male_new2_two"
"F" = "Female_new2_two";
RUN;
/*use stored defined format way 3 */
OPTIONS FMTSEARCH=(format);
DATA sample;
SET sashelp.class;
FORMAT sex GENDERsextwo. ;
RUN;
proc freq data=sample;
table sex;
run;
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
--------------------------------------------------------------------
Female_new2_two 9 47.37 9 47.37
Male_new2_two 10 52.63 19 100.00
9.4.5 Use stored defined format in batch- way 4
LIBNAME format "C:\Users\hed2\Downloads\code-storage\code\format";
PROC FORMAT LIBRARY=format ;
VALUE $GENDER
"M" = "Male_new"
"F" = "Female_new";
RUN;
/*use sas file to add format in batch*/
OPTIONS FMTSEARCH=(format);
DATA sample;
SET sashelp.class;
%INCLUDE 'C:\Users\hed2\Downloads\code-storage\code\format\use_format.sas';
RUN;
proc freq data=sample;
table sex;
run;
The FREQ Procedure
sex in class
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
--------------------------------------------------------------------
Female_new2_two 9 47.37 9 47.37
Male_new2_two 10 52.63 19 100.00
9.4.6 Modify format of the existing proc format
/*User-defined formats cannot be edited. to create a macro by myself ; or proc freq then use excel*/;
PROC FORMAT ;
VALUE $GENDER
"M" = "Male_new"
"F" = "Female_new";
RUN;
proc format;
value $sex_f "F"="Female_f" other=[5.1];
run;
DATA sample;
SET sashelp.class;
FORMAT sex $sex_f. ;
RUN;
proc freq data=sample;
table sex;
run;
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
-------------------------------------------------------------
Female_f 9 47.37 9 47.37
M 10 52.63 19 100.00
9.4.7 Transform missing to char then to numeric again
PROC FORMAT ;
VALUE $num
"M" = 1
"F" = 2;
RUN;
DATA sample_2;
SET sashelp.class;
sex_2 = input(put(sex ,num.),best.); /*Converting variable types and keep*/
RUN;
proc freq data=sample_2;
table sex_2;
run;
The FREQ Procedure
Cumulative Cumulative
sex_2 Frequency Percent Frequency Percent
----------------------------------------------------------
1 10 52.63 10 52.63
2 9 47.37 19 100.00
9.4.8 Output format of the existing format name by alphabet order
/*but we dont know the format name*/
/*PROC FORMAT FMTLIB;*/
PROC FORMAT ;
VALUE $GENDER
"M" = "Male_new"
"F" = "Female_new";
RUN;
proc format library = work.formats FMTLIB cntlout = cntlout;
select $GENDER;
run;
---------------------------------------------------------------------------
-
| FORMAT NAME: $GENDER LENGTH: 10 NUMBER OF VALUES: 2
|
| MIN LENGTH: 1 MAX LENGTH: 40 DEFAULT LENGTH: 10 FUZZ: 0
|
|--------------------------------------------------------------------------
|
|START |END |LABEL (VER. V7|V8
08MAR2024:09:19:51)|
|----------------+----------------+----------------------------------------
|
|F |F |Female_new
|
|M |M |Male_new
|
---------------------------------------------------------------------------
-
9.4.9 A macro to copy the existing proc format
%macro formatd(data, var);
data temp;
set &data;
keep &var;
run;
/*original freq*/
ods output OneWayFreqs=sample_b ;
proc freq data=temp ;
table &var /missing ;
run;
ods output close;
/*delete format freq*/
data sample2;
set temp;
Format _all_;
run;
proc freq data=sample2 ;
table &var /missing out=sample_c ;
run;
/*select original variable code*/
proc sql noprint;
select name into :firstvar_b from dictionary.columns where libname='WORK' and memname='SAMPLE_B'
and varnum=2;
quit;
data sample_b2;
set sample_b;
Keep &firstvar_b ;
run;
/*select original variable label*/
proc sql noprint;
select name into :firstvar_c from dictionary.columns where libname='WORK' and memname='SAMPLE_C'
and varnum=1;
quit;
data sample_c2;
set sample_c;
Keep &firstvar_c ;
run;
/*merge variable code and label*/
data sample_bc;
set sample_b2 (RENAME=(&firstvar_b=new_b));
set sample_c2 (RENAME=(&firstvar_c=new_c));
run;
/*create format format and output*/
data sample_bc;
set sample_bc;
Original_format = CATS(new_c,"=","'",new_b,"'");
run;
proc print data=sample_bc noobs;
var Original_format;
run;
/*data _null_;*/
/* set sample_bc ;*/
/* file print;*/
/* putlog Original_format @@;*/
/* put Original_format @@;*/
/*run;*/
%mend formatd;
PROC FORMAT ;
VALUE $GENDER
"M" = "Male_new"
"F" = "Female_new";
RUN;
DATA sample;
SET sashelp.class;
FORMAT sex $GENDER. ;
RUN;
%formatd(sample, sex );
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
---------------------------------------------------------------
Female_new 9 47.37 9 47.37
Male_new 10 52.63 19 100.00
The FREQ Procedure
Cumulative Cumulative
Sex Frequency Percent Frequency Percent
--------------------------------------------------------
F 9 47.37 9 47.37
M 10 52.63 19 100.00
Original_
format
F='Female_new'
M='Male_new'