Chapter 3: String Functions
String Functions
Command Function Example Output Remark
SUBSTRN(str, Extract substring from a given SUBSTRN('Hello World', 7, 5); 'World' *no end position,
start, length) string. will take all
behind
TRIM(str) Removes trailing spaces from TRIM(' SAS '); ' SAS'
a string.
TRIMN(str) Removes trailing blanks TRIMN(' Data '); ' Data'
exclusively
STRIP Removes both leading and STRIP 'SAS Code'
trailing spaces from a string. (' SAS Code ');
COMPRESS(str) Removes specific characters COMPRESS('123-45-6789', '-'); '123456789' *if no characters
from a string. are specified, it
removes spaces
by default.
COMPBL(str) Compresses multiple blanks COMPBL('SAS 'SAS
into a single blank. Programming Language'); Programmi
ng
Language'
FIND(str, value) Searches the value in the Returns 0 or the
given string starting position
INDEX(str, Find location of searchstr in
searchstr) str
LOWCASE(str) Change to lowercase
UPCASE(str) Change to uppercase
TRANWRD Replaces all occurrences of a TRANWRD (variable,
specific word in a given string replace what, replace with)
TRANSLATE Replaces specific characters TRANSLATE (variable,
in a given char expression replace what, replace with)
SCAN(str, n, dlm) Extract words in a value
marked by delimiters
Concatenation
Command Function Example Output Remark
CAT Concats without removing leading or trailing CAT('Hello', ' ', 'World'); 'Hello World'
spaces.
CATT Concats and removes trailing spaces from CATT('Hello', ' ', 'World'); 'HelloWorld'
each argument.
CATS Concates and removes leading and trailing CATS(' Hello ', ' World '); 'HelloWorld'
spaces from each argument.
CATX Concats, removes leading and trailing CATX( ',' , ' Hello ', ' World 'Hello,World
spaces from each argument, and inserts a '); '
specified delimiter between concatenated
strings.
Modifiers
Used with COMPRESS, FIND, and TRANWRD.
Command Function Example Output Remark
a Removes all alphabetic chars COMPRESS('123ABCabc!', '!', 'a'); '123!'
from String.
ak Keep only alphabets from COMPRESS('123ABCabc!'', 'ak'); ‘ABCabc’
String.
kd Keep only numeric values. COMPRESS('123ABCabc!', '', 'kd'); ‘123’
d Remove numerical values COMPRESS('123ABCabc!', '!', 'd'); 'ABCabc!'
from String.
i Remove specified chars FIND('Hello World', 'WORLD', 'i'); ‘7’ *(finds 'World'
from String. ignoring case)
k Keep the specified chars in COMPRESS('123ABCabc!', '123', 'k'); ‘123’
the string instead of
removing them.
l Remove lowercase chars COMPRESS('123ABCabc!', '123', 'k'); ‘'123ABC!’
from String.
p Remove punctuation chars COMPRESS('123ABCabc!', '', 'p'); '123ABCab
from String. c’
s Remove spaces from String. COMPRESS('123 ABC abc !', '', 's'); '123ABCabc!' *default setting
u Remove uppercase chars COMPRESS('123ABCabc!', '', 'u'); '123abc!
from String.
Chapter 4: Merging Tables and Loops
Merging Tables
Command Function Example Remark
merge Combines datasets based on a common key data merged_dataset; proc print
(usually an ID variable) merge dataset1 dataset2; data=combined;
by id; Run;
run;
*Add behind
set (SEPARATE) Appends one dataset to data combined; all variables included
another, creating a single dataset set test_result; /* table1 */ (may have NaN)
set test_result2; /* table3 */ Messy asf
run;
(COMBINED) Appends observations based on data combined;
dataset order set test_result
test_result2;
run;
sql inner Combines datasets based on a common key proc sql;
join using SQL create table
merged_dataset as
select *
from dataset1
inner join dataset2
on dataset1.id =
dataset2.id;
quit;
append Appends one dataset to another proc append
base=dataset1
data=dataset1;
run;
merge output
Set Separate output
Set Separate output
sql inner join output
append output
Loops
Command Function Example Output Remark
DO COUNTER IYKYK DATA table1;
END; do counter=1 to 4;
answer + 3;
output;
end;
run;
DO UNTIL() X = 1;
END DO UNTIL (x>3);
output;
X+1;
end;
DO WHILE() IYKYK DO WHILE (x<=3);
END output;
X+1;
end;
array Combines datasets data example; Temporary array only exists
based on a common array nums{3} _temporary_ within data step
key (usually an ID (10,20,30);
variable) array letters{3} _temporary_ ARRAY array-name (subscript)<$><array-
('A', 'B', 'C'); elements>
do i = 1 to 3;
num = nums{i};
letter = letters{i};
put num = letter=;
output;
end;
run;
Command Function Example Remark
IN Test if a value is present in the array’s row. If ‘A’ in x then ever_absent = ‘Yes’;
Else ever_absent = ‘No’;
OF Execute calculations on the full row of the Max_unit_sold = max(of sales(*));
array Min_unit_sold = min(of sales(*));
Chapter 5: Decision Making
Command Function Example Output Remark
IF
IF-THEN
ELSE
IF-THEN IF (con1) THEN result1;
ELSE-IF ELSE IF (con2) THEN result2;
ELSE IF (con3) THEN result3;
IF-THEN IF (con1) THEN DELETE;
DELETE
IF-THEN-D0 IFvar1 = “value” then do;
new1 = 10;
new2 = 5;
end;
Numeric Data Format
Command Function Example Output
PUT
INPUT Convert character variable to INPUT(str, informat)
numeric variable
DOLLARW.D Adds a dollar sign to numerical INPUT salary 5.; $5012.0
data INFORMAT salary 5.; 23056.0
FORMAT salary DOLLAR7.1;
DATALINES;
5012
23056;
COMMAW.D To add a comma to separate INPUT salary 5.; 5,012.0
the digits of numerical data INFORMAT salary 5.; 23056.0
FORMAT salary COMMA7.1;
DATALINES;
5012
23056;
PERCENTW.D percent 6.2;
Informat Use
W. “W” number of columns, no decimal
places
W.D. “W” number of columns with “D” decimal
places
● 7.2 > 4 spaces for the left, 1 for
the decimal, 2 for the right
Chapter 6: SAS logical operator, function, and replace values
Arithmetic Operators
Command Function
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
Logical Operators
Operator Description
& AND operator
| OR operator
~ NOT operator
Comparison Operators
Operator Description
= EQUAL operator
^= NOT EQUAL operator
< LESS THAN operator
<= LESS THAN EQUAL TO
> GREATER THAN
>= GREATER THAN EQUAL TO
IN If the value of the variable is equal to any one of the values given
in the list of values
*V1 in (5,7,8,9)
Minimum/Maximum Operators (Mathematical Functions)
Operator Description Example
MIN Returns the minimum value from the list of values. MIN(1,2,3);
MAX Returns the maximum value from the list of values. MAX(1.2, 1.5, 2.1);
MEDIAN Returns the median value by calculating all variable MEDIAN(V1, V2, V3);
values.
RANUNI Returns a random value. RANUNI(0)
RANDOM() Returns a random number between 0 and 1. RANDOM()
SQRT Returns the square root value of the sum of all SQRT(SUM(V1, V2, V3));
variable values.
ABS(V1) Returns the absolute value of the number. abs(15)
Date and Time Functions
Operator Description Example
INTICK(YEAR, Used to count the total number of years or months
date1, date2) between dates
WEEKDAY(MONTH Used to display the weekday on a particular date
, date1, date2)
TODAY() / DATE() Returns current date in SAS date format.
TIME() Returns current time in SAS time format.
Truncation Functions
Operator Description Example
CEIL(v1) Returns the greatest nearest value of the integer value. CEIL(var1)
FLOOR(v1) Returns the smallest nearest value of the integer value. FLOOR(var1)
ROUND(v1, round- Returns the nearest value of the integer value. ROUND(4567.89)
off-unit)
INT(v1) Returns the integer value. INT(23.41)
FUZZ(v1) Adjusts floating-point numbers to minimize the effects of FUZZ(0.0000000001)
rounding errors, especially when performing equality
comparisons between very small values.
TRUNC(number, Truncates a numeric value to a specified length TRUNC(12.34567, 2)
length)
Replace missing values
Operator Description Example
COALESCE returns the first non-missing value from a list of data example;
arguments var1 = .;
var2 = 10;
/* var3 will be set to 10, as it's the first non-missing value var3 = COALESCE(var1, var2,
*/ 0);
put var3=;
run;
ARRAY replace or transform values across multiple variables. data example;
set data_missing;
array vars {*} var1-var3;
do i = 1 to dim(vars);
if vars{i} = . then vars{i} = 0;
end;
drop i;
run;
STDIZE standardize numeric variables, but it also provides proc STDIZE
options to replace missing values using methods like data=data_with_missing
mean, median, or a custom value. out=example reponly
missing=0;
*/* Specify variables to replace missing values */ var var1-var3;
run;
Command Function Example Output Remark
u
noobs Removes observation DATA table1;
columns do counter=1 to 4;
title answer + 3;
output;
end;
run;
proc print NOOBS;
title 'Four Times Three
Equals';
RUN;
If () then do These values are added to data class;
end; the class dataset alongside set test_table4; /* create table
the original variables from */
else do; format stay category$ 30.;
test_table4
if age <=12 then do;
end;
fee = "15k";
stay = "allowed";
category="kid";
end;
else do;
fee = "25k";
stay = "not allowed";
category = "teenage";
end;
run;
format Specifies that the variables format stay category $ 30.;
stay and category should be
formatted as character
variables with a length of 30
characters.
set
put Converts a numeric variable data example;
to a character string num = 123;
char = PUT(num, 8.);
run;