The SAS mean() function allows you to find the mean value across columns and returns the average in a data step. data example; a = mean(1,4,7); put a; b = mean(1,.,-1); put b; run; /* Log Output */ 4 0 When working with different data in programming, the ability to be able to easily find […]
SAS
SAS sum() Function – Find Sum Across Columns in Data Step
The SAS sum() function allows you to find the total across columns and returns the sum of the arguments in a data step. data example; a = sum(1,4,7); put a; b = sum(1,.,-1); put b; run; /* Log Output */ 7 1 When working with different data in programming, the ability to be able to […]
SAS max() Function – Find Maximum Value Across Columns in Data Step
The SAS max() function allows you to find the maximum value across columns and returns the biggest number in a data step. data example; a = max(1,4,7); put a; b = max(1,.,-1); put b; run; /* Log Output */ 7 1 When working with different data in programming, the ability to be able to easily […]
SAS min() Function – Find Minimum Value Across Columns in Data Step
The SAS min() function allows you to find the minimum value across columns and returns the smallest number in a data step. data example; a = min(1,4,7); put a; b = min(1,.,-1); put b; run; /* Log Output */ 1 -1 When working with different data in programming, the ability to be able to easily […]
Using SAS to Find Mean by Group with PROC MEANS
To find the mean by group in SAS, you can use PROC MEANS and specify by group in the CLASS statement. You can find the mean of multiple variables and group by multiple groups with PROC MEANS. data example; input group $ value; datalines; A 1 A 2 A 3 B 4 B 5 B […]
Using SAS to Sum by Group with PROC MEANS
To sum by group in SAS, you can use PROC MEANS and specify by group in the CLASS statement. You can sum multiple variables and group by multiple groups with PROC MEANS. data example; input group $ value; datalines; A 1 A 2 A 3 B 4 B 5 B 6 C 7 C 8 […]
Identifying Duplicates in SAS with PROC SORT dupout Option
To identify duplicates in SAS, you can use PROC SORT and use the dupout option. ‘dupout’ will create a new dataset and keep just the duplicate observations of the original dataset. data example; input a b; datalines; 1 2 1 2 1 2 2 6 2 6 2 6 2 8 ; run; proc sort […]
nodup vs nodupkey PROC SORT Options in SAS
When working with data, the ability to remove duplicates from your data can be very valuable. PROC SORT gives many different options for you to use which can change the behavior of what PROC SORT does. Two options which are useful in PROC SORT which allow you to remove duplicates are ‘nodup’ and ‘nodupkey’. In […]
SAS nodupkey – How to Remove Duplicates with PROC SORT by Key
When using PROC SORT in SAS, you can use the ‘nodupkey’ option to remove observations with duplicate BY values. In other words, you can remove duplicates by key variables. data example; input a b; datalines; 1 2 1 3 1 4 2 5 2 6 2 7 2 8 ; run; proc sort data=example nodupkey; […]
SAS call symput – Create Macro Variables in Data Step
In SAS, the call symput function allows you to create macro variables in a SAS data step. You can assign both numeric and character values to macro variables with call symput. data example; call symput('var1', "example value"); call symput('var2', 2); run; %put &var1; /* Log Output: */ example value 2 When working with SAS, the […]
Change Length of Character Variable in SAS Dataset
To change the length of a character variable in SAS, you can use the LENGTH statement before the SET statement in a data step. data want; length name $30; set have; run; You can change the length of one or more character variables with the LENGTH statement. data want; length name second_name third_name $30; set […]
yyyy-mm-dd Date Format in SAS
To format a date variable in SAS like yyyy-mm-dd, you can use the yymmdd10. date format in a data step. data example; d = "15sep2022"d; put d yymmdd10.; run; // Log Output: 2022-09-15 When working with different variables in SAS, the ability to change formats easily is valuable. One such case is if you have […]
Date Format ddmmmyyyy in SAS
To format a date in SAS like ddmmmyyyy, you can use the date9. date format. data example; d = "15sep2022"d; put d date9.; run; // Log Output: 15SEP2022 When working with different variables in SAS, the ability to change formats easily is valuable. One such case is if you have a date and want to […]
SAS Month Year Format monyyw.
To format a date variable with a month and year format in SAS, you can use the monyy5. or monyy7. date formats. data example; d = "15sep2022"d; put d monyy5.; put d monyy7.; run; // Log Output: SEP22 SEP2022 When working with different variables in SAS, the ability to change formats easily is valuable. One […]
Get Last Observation of SAS Dataset with end=
To get the last observation of a dataset, you can use the end= data set option. end= creates a temporary numeric value whose value is used to detect the last observation. data last_obs_only; set all_data end=last_obs; if last_obs; run; When working with datasets in any programming language, the ability to get snapshots of your data […]
How to Select First 100 Observations of SAS Dataset
To select the first 100 observations of a dataset in SAS, you can use the obs= data step set option and pass 100. data first_100_obs; set all_data(obs=100); run; You can also use the SAS automatic variable _n_ to get the first 100 observations from a dataset. data first_100_obs; set all_data; if _n_ <= 100 then […]
Set Multiple Datasets in SAS Data Step
In SAS, you can set multiple datasets in a single data step. If you have multiple datasets in a set statement, you will combine the datasets vertically. data new; set dataset1 dataset2; run; When working with data in SAS, the ability to create new datasets is valuable. Various operations, such as merging and appending, allow […]
SAS yymmdd10. Date Format
To format a date with the yymmdd10 format in SAS, you can format a date variable like ‘YYYY-MM-DD’. data example; d = "14sep2022"d; put d yymmdd10.; run; // Log Output: 2022-09-14 If you want to format a date like ‘YYYY/MM/DD” then you can use yymmdds10 in a data step. data example; d = "14sep2022"d; put […]
SAS include – Execute Code from Other Files in SAS with %include
The SAS %INCLUDE statement allows you to bring in code from other SAS files and execute them. This allows you to create modular code which is portable and reusable. %include "path/to/other/file.sas"; When working in SAS, it is not obvious how to create modular code when in other languages, there are many ways to import modules […]
SAS rename Statement – How to Rename Variables in a Data Step
To rename variables in a SAS data step, you can use the RENAME statement. With the RENAME statement, you can rename one or multiple variables in a data step. You can rename variables with RENAME in a few different spots in a data step. The first place you can use RENAME is in the SET […]
SAS data _null_ – Create a SAS Dataset with No Records and Variables
In SAS, the _null_ keyword in a SAS Data Step allows us to create a SAS dataset with no records and variables. With _null_, surprisingly, we are able to many things in our SAS code. In this post, you will learn the many benefits of using _null_ in your SAS code. Using the SAS _null_ […]
How to Combine Datasets Vertically in SAS
To combine datasets vertically in SAS, the easiest way is to “set” the datasets in a SAS data step. data new; set dataset1 dataset2; run; You can also use PROC APPEND to append a dataset at the end of another dataset. proc append base=dataset1 data=dataset2; run; When working with data in SAS, the ability to […]
SAS %eval() Function – Evaluate Expressions in SAS Macro
When working in the SAS Macro Language, you can use the %eval() function to evaluate logical expressions and perform integer arithmetic. %let a = 3+5; %let b = 10/5; %let c = 10>3; %let eval_a = %eval(&a); %let eval_b = %eval(&b); %let eval_c = %eval(&c); %put &eval_a; %put &eval_b; %put &eval_c; /* Output */ 8 […]
SAS Remove Formats from Dataset with PROC DATASETS
To remove all formats from a SAS data you can use PROC DATASETS to remove all formats using the MODIFY statement and ATTRIB option. proc datasets lib=work; modify example_dataset; attrib _all_ format=''; run; If you want to remove the format of just one variable, you can just specify that variable after ATTRIB. proc datasets lib=work; […]
SAS Remove Labels from Dataset with PROC DATASETS
To remove all labels from a SAS data you can use PROC DATASETS to remove all labels using the MODIFY statement and ATTRIB option. proc datasets lib=work; modify example_dataset; attrib _all_ label=''; run; If you want to remove the label of just one variable, you can just specify that variable after ATTRIB. proc datasets lib=work; […]
Multiple Condition If Statements in SAS Macro Language
In the SAS Macro Language, you can easily create if statements with multiple conditions. %macro conditional; /* if you only need 1 line for the "then" expression */ %if &var = 1 %then /* do something */; %else %if &var = 2 %then /* do something else */; %else /* do something else */; /* […]
SAS If Then Statements with Multiple Variables
You can easily create if then statements with multiple variables using the logical operators or and and in a SAS data step. data k; a = 3; b = 5; if a > 5 or b < 10 then put "success"; run; /* Output */ success If you are using the SAS Macro language, you […]
SAS _n_ – How to Use the Automatic Variable _n_ in a Data Step
The SAS automatic variable _n_ represents the number of times the data step has iterated. As an automatic variable, _n_ is created automatically by SAS when a data step is performed. _n_ is temporary, meaning it is not kept on the dataset after the data step is finished. When a data step starts, _n_ is […]
Get the Row Number in a SAS Data Step with _n_
To get the row number in a SAS data step, you can use the SAS automatic variable _n_. data want; set have; row_number = _n_; run; When working with data in SAS, the ability to easily get information about rows or columns is valuable. One piece of information which can be valuable to know is […]
SAS Power Function – Exponentiate Numbers with ** in a Data Step
In SAS, there is no power() function. To perform exponentiation as well as find the roots of numbers in a SAS data step, you can use **. data have; a = 10; b = a**2; put b=; run; /* Output */ b=100 SAS allow us to perform many different kinds of calculations in our code […]
SAS contains() – Check if Variable Contains a String in Where Statement
In SAS, we can check if a variable contains a specific string with the contains operator in a where statement. data want; set have; where variable contains "something"; run; When working in SAS, the ability to easily be able to create complex filters and get the subsets we desire is valuable. One such filter which […]
SAS where in – Subset Data by Multiple Values in Data Step
When filtering a SAS dataset, you can filter by multiple values with the in operator in a where statement. data want; set have; where variable_a in (1, 2, 3); run; When working in SAS, the ability to easily be able to create complex filters and get the subsets we desire is valuable. One such filter […]
SAS Less Than or Equal to with LE or <=
The SAS less than or equal to operators LE and
SAS Greater Than or Equal to with GE or >=
The SAS greater than or equal to operators GE and >= operators allow us to check if a variable is greater than or equal to another value in a SAS data step. data k; a = 3; if a ge 2 then put 'a greater than or equal to 2 with ge'; if a >= […]
SAS Not In – How to Check if Variable is Not in List of Values
To check if a variable’s value is not in a list of values, we can use the SAS not operator in combination with the in operator. data k; a = 4; if a NOT IN (1, 2, 3) then put 'a is not in (1, 2, 3)'; run; /* Output: */ a is not in […]
SAS Not Equal – Check if a Variable is Not Equal to Another in Data Step
There are three operators for ‘not equal’ in SAS. You can use ne, ^=, or ~= to check if a variable is not equal to another variable or value. data k; a = 'string'; if a ne 'another string' then put 'a not equal to "another string" with ne'; if a ^= 'another string' then […]
SAS calculated – Use Columns Created from Select in PROC SQL
When using PROC SQL in SAS, calculated allows you to use columns created from calculations in a select clause in the having clause or another variable calculation. proc sql; create table new_table as select id, name, sum(sales) as total_sales from old_table group by name where calculated total_sales > 100 order by name; quit; PROC SQL […]
SAS right() Function – Right Align Character Variables in Data Step
The SAS right() function gives us the ability to right align character variables. data k; a = 'this is a string with some trailing blanks '; b = right(a); put b=; run; /* Output: */ a=this is a string with some trailing blanks b= this is a string with some trailing blanks When working with […]
SAS left() Function – Left Align Character Variables in Data Step
The SAS left() function gives us the ability to left align character variables. data k; a = ' this is a string with some leading blanks'; b = left(a); put b=; run; /* Output: */ a= this is a string with some leading blanks b=this is a string with some leading blanks When working with […]
SAS tranwrd() Function – Replace Characters in Strings in SAS Data Step
To replace all occurrences substrings in character variables when working in SAS, you can use the SAS tranwrd() function. data k; a = 'this is a string with some characters and words'; b = tranwrd(a,"t","mm"); put b=; run; /* Output: */ b=mmhis is a smmring wimmh some characmmers and words When working with character variables […]
Round Number to Nearest Integer in SAS
To round a number to the nearest integer in SAS, you can use the SAS round() function. By default, round() rounds to the nearest integer. data data_with_rounding; set data; round_to_integer = round(num); run; When working with numbers in SAS, the ability to round and keep a certain number of decimal places is valuable. To round […]
SAS substr() Function – Get Substring from Character Variable
To get a substring of a character variable in a SAS data step, you can use the SAS substr() function. The substr() function takes arguments which define the starting position of the substring and how many characters you want to get from the string. data k; var = "string"; first_two_chars = substr(var, 1, 2); put […]
Get Substring from Right in SAS
To get a substring from the right in a SAS data step, you can use the SAS substr() function and specify a start position with help from the length function. data k; var = "string"; last_two_chars = substr(var, length(var) – 2,2); all_but_last_two = substr(var, 1, length(var) – 2); put substr_from_right=; run; /* Output */ last_two_chars=ng […]
Concatenating Strings in a SAS Data Step
To concatenate strings in a SAS data step, the easiest way is to use the SAS catx() function. data k; var1 = "This"; var2 = "is"; var3 = "a"; var4 = "string."; concatenated = catx(" ",var1, var2, var3, var4); put concatenated=; run; /* Output */ concatenated=This is a string. When working with character variables and […]
SAS find() Function – Check if Substring is in Character Variable
The SAS find() function allows us to check if a given substring of characters is in a character variable in a SAS data step. data k; a = 'this is a string with some characters'; b = find(a,"str"); put b=; run; /* Output: */ b=11 When working with datasets which contain string and character variables, […]
Remove Specific Character from String in SAS
When working with strings in SAS, you can remove specific characters from a string with the SAS compress() function. For example, if we wanted to remove the letters “a” and “b” from a string, we could do so with the following SAS code. data k; a = 'Alfred and Betty went to the beach to […]
Round Number to 2 Decimal Places in SAS
To round a number to 2 decimal places in SAS, you can use the SAS round() function and pass ‘0.01’ for the second argument. data data_with_rounding; set data; round_to_two_decimals = round(num, 0.01); run; When working with numbers in SAS, the ability to round and keep a certain number of decimal places is valuable. To round […]
Do Loop in SAS Macro Language
When using the SAS Macro language, we can easily create do loops. The syntax for a SAS Macro do loop is shown below. %macro example; %do i = 1 %to 10; /* do stuff here */ %end; %mend; The SAS Macro language is incredibly powerful and allows us to build dynamic programs. One common piece […]
SAS let – Create User-Defined Macro Variables in Your SAS Code
We use the SAS let statement to define macro variables to use in our code. The variables created are character strings or text expressions. %let var1 = This is a macro variable defined by the SAS let statement; The SAS Macro Language is very powerful and allows us to create dynamic SAS programs. The basis […]
SAS prxmatch – Find Pattern in String Using Regex (Regular Expression)
You can use the SAS prxmatch function to perform a regular expression (regex) search on a character variable in a SAS data step, and return the position where the pattern first was found. data data_new; string = "This is a string with some text that we will search and replace with a regex expression."; pat […]
SAS select when – Evaluating Character Values for Conditional Processing
With the SAS select statement, we can use a character variable and perform conditional processing on it’s values. data data_new; set data; select(age_indicator); when('Child') price = 5; when('Senior') price = 10; otherwise price = 12; end; run; When working with data, conditional processing is very useful for defining new variables with logic. In SAS, one […]
SAS if then else – Write Conditional Expressions for Multiple Conditions
To do conditional processing in a SAS Data Step, the easiest way is with if then else statements. data data_new; set data; length legal_description $ 50.; if age < 18 then legal_description = "Can't Drink or Smoke"; else if age < 21 then legal_description = "Can't Drink, but Can Smoke"; else legal_description = "Can Drink […]
SAS case when – Conditional Logic with case Expression in PROC SQL
If we want to do conditional processing in a PROC SQL procedure, we can use the SAS case expression. For the conditions, we use when. proc sql; select name, age, case age when < 18 then "Can't Drink or Smoke" when > 21 then "Can Drink and Smoke" else "Can't Smoke, but Can Smoke" end […]
catx SAS – Concatenate String Variables with Delimiter in SAS Data Step
To concatenate character variables with a delimiter, the easiest way is with the catx() SAS function. data k; var1 = "This"; var2 = "is"; var3 = "a"; var4 = "string."; concatenated = catx(" ",var1, var2, var3, var4); put concatenated=; run; /* Output */ concatenated=This is a string. If you are working with the SAS Macro […]
SAS strip – Remove All Leading and Trailing Blanks from String Variable
To remove all leading and trailing blanks from a string variable in a SAS data step, we can use the SAS strip() function. data k; a = ' this is a string with some leading and trailing blanks '; b = "*" || trim(a) || "*"; put b=; run; /* Output: */ b=*this is a […]
SAS trim – Remove All Trailing Blanks from String Variable in Data Step
To remove all trailing blanks from a string variable in a SAS data step, we can use the SAS trim() function. data k; a = 'this is a string with some trailing blanks '; b = trim(a) || "*"; put b=; run; /* Output: */ b=this is a string with some trailing blanks* When working […]
SAS Lowercase – Make String Lowercase with SAS lowcase function
To make a string variable’s letters all lowercase in a SAS data step, we can use the SAS lowcase() function. data data_new; set data; lowercase_word = lowcase(word); run; When working with strings in our datasets, it can be useful for comparison or display purposes to convert strings to have all uppercase or lowercase letters. In […]
SAS Proper Case – Capitalize First Letter of Word with propcase Function
To capitalize the first letter of a word and make a string variable’s letters all proper case in a SAS data step, we can use the SAS propcase() function. data data_new; set data; proper_case_word = propcase(word); run; When working with strings in our datasets, it can be useful for comparison or display purposes to convert […]
SAS Uppercase – Make String Letters Uppercase with SAS upcase function
To make a string variable’s letters all uppercase in a SAS data step, we can use the SAS upcase() function. data data_new; set data; uppercase_word = upcase(word); run; When working with strings in our datasets, it can be useful for comparison or display purposes to convert strings to have all uppercase or lowercase letters. In […]
intcx SAS – Find Time Periods Between Two Dates in SAS Data Step
To find the time periods between two dates in a SAS data step, we can use the SAS intcx() function. The return value of intcx() is an integer. data data_new; set data; num_days_between_d1_d2 = intck('day',d1,d2); num_months_between_d1_d2 = intck('month',d1,d2); num_years_between_d1_d2 = intck('years',d1,d2); run; When working with dates in our data, it’s very useful to be able […]
SAS intnx – Add or Subtract Time from Date Variables in SAS Data Step
To add or subtract time from a date in a SAS data step, we can use the SAS intnx() function. data data_new; set data; date_plus_1_day = intnx('day', date_variable, 1, 'same'); date_plus_1_mon = intnx('month', date_variable, 1, 'same'); date_plus_1_yr = intnx('year', date_variable, 1, 'same'); run; When working with dates in our data, it’s very useful to be […]
SAS Percent Format – Formatting Number as Percent in SAS Dataset
In SAS, we can format numbers as percentages very easily using the built-in SAS PERCENTw.d format or creating our own format with PROC Format. /* Using Built-in SAS PERCENTw.d format */ data data_new; format num dollar10.2; set data; run; /* Creating our own percent format */ proc format; picture our_percent_format(round) low -< 0 = '000009.9%' […]
SAS Dollar Format – Formatting Numbers as Dollars in SAS Dataset
In SAS, we can format numbers as dollars very easily using the built-in SAS DOLLARw.d format or creating our own format with PROC Format. /* Using Built-in SAS DOLLARw.d format */ data data_new; format dollar_amount dollar10.2; set data; run; /* Creating our own dollar format */ proc format; picture our_dollar_format low – -1000000000 = '00,000,000,009.9 […]
PROC Format – Create User-Defined Formats for Variables in SAS
The format procedure, PROC FORMAT, allows us to create user-defined formats for our variables in SAS. PROC format lets us define a map which will print variables differently values based on their current value. proc format; value gender 'M' = 'Male' 'F' = 'Female' other = 'N/A' ; run; PROC format is one of the […]
countw SAS – Count Number of Words in a String
To count the number of words in a string, we can use the SAS countw() function. We can use countw() in a data step or with the SAS Macro Language. data data; string = "This is a string for an example"; x = countw(string); /* x = 7 */ run; %let string = This is […]
SAS scan Function – Return nth Word from Character String
In SAS, we can use the SAS scan() function to parse strings and extract words from the location we desire. For example, we can use the SAS scan() function to find the nth word in any character string. data data; string = "This is a string for an example"; x = scan(string,1); /* x = […]
mod Function in SAS – Find Remainder of 2 Numbers After Division
In SAS, we can use the mod() function to calculate the remainder of 2 numbers after division. You can use the SAS mod() function in a SAS data step, in PROC SQL, or in the macro language. data data_new; set data; remainder = mod(var1,divisor); run; proc sql noprint; select mod(num,divisor) into 😡 from data; quit; […]
SAS weekday function – Get Day of Week from Date Variable
To get the day of the week of a date variable in a SAS data step, the easiest way is to use the SAS weekday() function. data data_with_weekday; set data_with_dates; day = weekday(d); run; When working with data, many times we are working with dates and need to make adjustments to our data depending on […]
SAS year function – Get Year from Date Variable
To get the year of a date variable in a SAS data step, the easiest way is to use the SAS year() function. data data_with_year; set data_with_dates; yr = year(d); run; When working with data, many times we are working with dates and need to make adjustments to our data depending on which year something […]
SAS month function – Get Month from Date Variable
To get the month of a date variable in a SAS data step, the easiest way is to use the SAS month() function. data data_with_month; set data_with_dates; mo = month(d); run; When working with data, many times we are working with dates and need to make adjustments to our data depending on which month something […]
SAS Comments – Commenting Your SAS Code the Right Way
When writing programs in any programming language, comments can be useful to add further context to the code. In SAS, there are a few ways you can comment code in your programs. *This is a comment; %*This is a second comment; /*This is a third comment*/ /*This is a fourth multi-line comment*/ There are three […]
SAS round – Rounding Numbers in a SAS Data Step
Rounding numbers in a SAS data step can be easily done with the SAS round() function. data data_with_rounding; set data; round_to_ten = round(num,10); /* rounds to nearest ten */ round_to_integer = round(num); /* rounds to nearest integer */ round_to_tenth = round(num,0.1); /* rounds to nearest tenth */ run; If you want to round up, you […]
SAS floor – Round Down to Floor of Number in a SAS Data Step
To find the floor of a number in a SAS data step, the easiest way is to use the SAS floor() function. data data_with_floor; set data; floor = floor(num); run; When working with data, sometimes we have the need to round numbers down to their floor. In SAS, we can round numbers down to their […]
SAS ceil – Round Up to Ceiling of Number in a SAS Data Step
To find the ceiling of a number and round up in a SAS data step, the easiest way is to use the SAS ceil() function. data data_with_ceiling; set data; ceiling = ceil(num); run; When working with data, sometimes we have the need to round numbers up to their ceiling. In SAS, we can round numbers […]
SAS rand – Generate Random Numbers in a SAS Data Step
If you want to generate random numbers in a SAS data step, the easiest way is to use the SAS rand() function. data k; do i = 1 to 10; rand_num = rand("Uniform"); output; end; run; When working with data, sometimes it can be very useful to generate random numbers to be able to perform […]
SAS compress – Remove Whitespace and Characters from String
To remove whitespace or characters from a string in a SAS data step, we can use the SAS Compress function. By default, the SAS compress function removes all spaces from a string. data k; a = 'this is a string with some blanks'; b = compress(a); put b=; run; /* Output: */ b=thisisastringwithsomeblanks The SAS […]
SAS Delete Dataset – Use PROC Datasets to Delete SAS Files
To delete a dataset in SAS, the easiest way is to use PROC datasets and the DELETE statement: proc datasets library=work; delete dataset1; run; When working with data in SAS, sometimes we want to delete datasets to clear up space in our working directory or on our data servers. We can easily delete datasets in […]
IN in SAS – Checking if Variable is in Array of Values
Being able to see if a value is in an array can be very useful when programming. Using IN in SAS is very useful and allows us as SAS programmers to write concise code. In this post, you will learn how to use IN in SAS, and learn how you can use IN with SAS […]