Learn SAS Programming by Code Examples

Learn SAS Programming by Code Examples with me as I compiled a list of useful code samples below used in developing SAS applications for printing reports and manipulating records in datasets.

How to print Hello World in SAS?
Use ‘put’ statement to display message in SAS log or print into a report line. ‘file’ is a SAS keyword to output your ‘put’ message whereas ‘infile’ is a SAS keyword to derive/extract raw data from an external file or datalines. ‘print’ is also a SAS keyword that refers to either SAS internal display or external file if explicitly link to it. A _null_ dataset is a temporary dataset that will be not be keep any records after usage.

How to populate a dataset using external input?
Assuming you have a text file named as ‘hardware.txt’ containing hardware information, you can use ‘infile’ (SAS Keyword) to extract data from it. Once you have pinpoint the source, use ‘input’ (SAS Keyword) to define the fields which will break a line of record into multiple sections and dump data into its respective fields based on the variable length (using $. or d.) and position (using @). The dollar sign represents string variable whereas non-dollar sign represents numeric variable.

How to populate a dataset using datalines? (basic method)
Datalines is a quick method to define a list of records without the need to create an external file. It’s best use to test a dataset whether its working fine with other logic within your SAS code. Using datalines without a delimiter, by default, will separate each information based on whitespace. However, if those related information should be joined with whitespace, you will need to use delimiter.

Length is used here to set the maximum characters that a string variable can hold the value. By default, a string variable can only hold 8 characters. If you do not specify sufficient character to hold the string value, it will be truncated from right for string and truncated left for numeric value.

How to populate dataset using datalines with comma or specific delimiter?
Sometimes your record must be broken up using a symbol that is not used within a record. Whitespace is a common character that is used to link English words as a sentence, therefore we can’t use that as our delimiter. So we can use something else, which is comma. If your sentence includes comma and whitespace, you can use something non-common such as a slash or straight bar or perhaps semicolon.

How to limit observations read from external input?
Assuming the external file called hardware.txt has a list of over thousand of records/lines, but you just want the first 100 records/observations, then use (obs=total-observations-to-be-read).

How to limit observations read from a dataset?
The code below limits total observation read from dataset_A to 100. (obs=100)
Only the first 100 observations are processed in the data _null_;

How to limit observations read from an external file?
The code below limits total observation read from external textfile by 100 (obs=100)
Also, it starts processing observations at 50 and above (firstobs=50)

How to use Regular Expression to Match a String

How to use Regular Expression to Replace a String
Assuming that we want to change the value 1 into 2:

How to debug a dataset quickly?
To quickly check whether your variables in a dataset is populated correctly, you can use “proc print data” to view its contents as shown below, but don’t forget to print it out somewhere such as SASLIST.

How to truncate numeric data without rounding up or down?
Sometimes when you have numbers with more than 2 decimal points in your input or raw file, but you only want to display a maximum of 2 digits (2 decimal points) without rounding up or rounding down, we can use int() in SAS to solve this.

How to transfer data from one dataset to the other?
How to make a data in 1 dataset available in other datasets?

To allow a variable in one dataset available to be use in other datasets, you can use SAS function “call symput”.

How to merge 2 datasets using a common variable/attribute?
If you have 2 datasets that you want to combine by using a common field, you can use SAS “merge + by statement”. This will combine to 2 datasets based on the attribute you have given, acting as the key (index). If these key matches with one another, it will merge the record. As a result, one of the record will contains all fields from both datasets.

How to only output matched records when merging 2 datasets?

How to perform a simple conditional statements in SAS?
Below is an example of how to use a simple if-statement in SAS to compare numeric values stored in variables. SAS is similar to COBOL where it can also use two-letters notation instead of the usual high-level programming language comparison value like Java or C++.

How to perform multi-line conditional statements in SAS?
If you want to execute multiple actions within 1 if-statement, you will need to encapsulate them in between a do-end statement. Here is the syntax: if (conditional-true) then do; (actions); end;

How to string/join multiple conditions within an if-statement?
You can join multiple conditions within a conditional statement using AND or OR. If any of the conditions are reported false between AND, it will not run the actions within do-end statement. If any of the conditions are reported true between OR, it will run the actions within do-end statement. However, when using both AND-OR, you will need to use round brackets to determine the condition priority to check first.
If condition-1 is true and condition-2 is true, it will run.
if condition-1 is false and condition-2 is true, it will not run.
if condition-1 is false or condition-2 is false, it will not run.
if condition-1 is false or condition-2 is true, it will run.
if (condition-1 is true and condition-2 is false) or condition-3 is true; it will run.
if (condition-1 is true or condition-2 is false) and condition-3 is false; it will not run.

Leave a Reply

Your email address will not be published. Required fields are marked *