Strings Manipulation and Arrays in SAS

Last updated on Dec 13 2021
Vaidehi Reddy

Table of Contents

Strings Manipulation and Arrays in SAS

Strings in SAS are the values which are enclosed with in a pair of single quotes. Also the string variables are declared by adding a space and $ sign at the end of the variable declaration. SAS has many powerful functions to analyze and manipulate strings.

Declaring String Variables

We can declare the string variables and their values as shown below. In the code below we declare two-character variables of lengths 6 and 5. The LENGTH keyword is used for declaring variables without creating multiple observations.

data string_examples;
LENGTH string1 $ 6 String2 $ 5;
/*String variables of length 6 and 5 */
String1 = 'Hello';
String2 = 'World';
Joined_strings =  String1 ||String2 ;
run;
proc print data = string_examples noobs;
run;

On running the above code we get the output which shows the variable names and their values.

image001 21
String

String Functions

Below are the examples of some SAS functions which are used frequently.

SUBSTRN

This function extracts a substring using the start and end positions. In case of no end position is mentioned it extracts all the characters till end of the string.

Syntax

SUBSTRN('stringval',p1,p2)

Following is the description of the parameters used −

  • stringval is the value of the string variable.
  • p1 is the start position of extraction.
  • p2 is the final position of extraction.

Example

data string_examples;
LENGTH string1 $ 6 ;
String1 = 'Hello';
sub_string1 = substrn(String1,2,4) ;
/*Extract from position 2 to 4 */
sub_string2 = substrn(String1,3) ;
/*Extract from position 3 onwards */
run;
proc print data = string_examples noobs;
run;

On running the above code, we get the output which shows the result of substrn function.

image002 26
SUBSTRN

TRIMN

This function removes the trailing space form a string.

Syntax

TRIMN(‘stringval’)

Following is the description of the parameters used −

  • stringval is the value of the string variable.
data string_examples;
LENGTH string1 $ 7  ;
String1='Hello  ';
length_string1 = lengthc(String1);
length_trimmed_string = lengthc(TRIMN(String1));
run;
proc print data = string_examples noobs;
run;

On running the above code we get the output which shows the result of TRIMN function.

image003 19
TRIMN

SAS – Arrays

Arrays in SAS are used to store and retrieve a series of values using an index value. The index represents the location in a reserved memory area.

Syntax

In SAS an array is declared by using the following syntax −

ARRAY ARRAY-NAME(SUBSCRIPT) ($) VARIABLE-LIST ARRAY-VALUES

In the above syntax −

  • ARRAY is the SAS keyword to declare an array.
  • ARRAY-NAME is the name of the array which follows the same rule as variable names.
  • SUBSCRIPT is the number of values the array is going to store.
  • ($) is an optional parameter to be used only if the array is going to store character values.
  • VARIABLE-LIST is the optional list of variables which are the place holders for array values.
  • ARRAY-VALUES are the actual values that are stored in the array. They can be declared here or can be read from a file or dataline.

Examples of Array Declaration

Arrays can be declared in many ways using the above syntax. Below are the examples.

# Declare an array of length 5 named AGE with values.

ARRAY AGE[5] (12 18 5 62 44);

 

# Declare an array of length 5 named COUNTRIES with values starting at index 0.

ARRAY COUNTRIES(0:8) A B C D E F G H I;

 

# Declare an array of length 5 named QUESTS which contain character values.

ARRAY QUESTS(1:5) $ Q1-Q5;

 

# Declare an array of required length as per the number of values supplied.

ARRAY ANSWER(*) A1-A100;

Accessing Array Values

The values stored in an array can be accessed by using the print procedure as shown below. After it is declared using one of the above methods, the data is supplied using DATALINES statement.

DATA array_example;

INPUT a1 $ a2 $ a3 $ a4 $ a5 $;

ARRAY colours(5) $ a1-a5;

mix = a1||'+'||a2;

DATALINES;

yello pink orange green blue

;

RUN;

PROC PRINT DATA = array_example;

RUN;

When we execute above code, it produces following result −

image004 12
Access

Using the OF operator

The OF operator is used when analysing the data forma an Array to perform calculations on the entire row of an array. In the below example we apply the Sum and Mean of values in each row.

DATA array_example_OF;

INPUT A1 A2 A3 A4;

ARRAY A(4) A1-A4;

A_SUM = SUM(OF A(*));

A_MEAN = MEAN(OF A(*));

A_MIN = MIN(OF A(*));

DATALINES;

21 4 52 11

96 25 42 6

;

RUN;

PROC PRINT DATA = array_example_OF;

RUN;

When we execute above code, it produces following result −

image005 7
OF operator

Using the IN operator

The value in an array can also be accessed using the IN operator which checks for the presence of a value in the row of the array. In the below example we check for the availability of the colour “Yellow” in the data. This value is case sensitive.

DATA array_in_example;

INPUT A1 $ A2 $ A3 $ A4 $;

ARRAY COLOURS(4) A1-A4;

IF 'yellow' IN COLOURS THEN available = 'Yes';ELSE available = 'No';

DATALINES;

Orange pink violet yellow

;

RUN;

PROC PRINT DATA = array_in_example;

RUN;

When we execute above code, it produces following result −

image006 5
IN operator

So, this brings us to the end of blog. This Tecklearn ‘Strings Manipulation and Arrays in SAS’ blog helps you with commonly asked questions if you are looking out for a job in SAS. If you wish to learn SAS and build a career in Data Analytics domain, then check out our interactive, SAS Training for SAS BASE Certification Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/sas-training-for-sas-base-certification/

SAS Training for SAS BASE Certification Training

About the Course

SAS Certification Training is intended to make you an expert in SAS programming and Analytics. You will be able to analyse and write SAS code for real problems, learn to use SAS to work with datasets, perform advanced statistical techniques to obtain optimized results with Advanced SAS programming.  In this SAS online training course, you will also learn SAS macros, Machine Learning, PROC SQL, procedure, statistical analysis and decision trees. You will also work on real-life projects and prepare for the SAS Certified Base Programmer certification exam. Upon the completion of this SAS online training, you will have enough proficiency in reading spreadsheets, databases, using SAS functions for manipulating this data and debugging it.

Why Should you take SAS Training?

  • The average salary for a Business Intelligence Developer skilled in SAS is $100k (PayScale salary data)
  • SAS, Google, Facebook, Twitter, Netflix, Accenture & other MNCs worldwide are using SAS for their Data analysis activities and advance their existing systems.
  • SAS is a Leader in 2017 Gartner Magic Quadrant for Data Science Platform.

What you will Learn in this Course?

Introduction to SAS 

  • Introduction to SAS
  • Installation of SAS
  • SAS windows
  • Working with data sets
  • Walk through of SAS windows like output, search, editor etc

SAS Enterprise Guide

  • How to read and subset the data sets
  • SET Statement
  • Infile and Infile Options
  • SAS Format -Format Vs Informat

SAS Operators and Functions

  • Using Variables
  • Defining and using KEEP and DROP statements
  • Output Statement
  • Retain Statement
  • SUM Statement

Advanced SAS Procedures

  • PROC Import
  • PROC Print
  • Data Step Vs Proc
  • Deep Dive into Proc

Customizing Datasets

  • SAS Arrays
  • Useful SAS Functions
  • PUT/INPUT Functions
  • Date/Time Functions
  • Numeric Functions
  • Character Functions

SAS Format and SAS Graphs

  • SAS Format statements
  • Understanding PROC GCHART, various graphs, bar charts: pie, bar

Sorting Techniques

  • NODUP
  • NODUKEY
  • NODUP Vs NODUKEY

Data Transformation Function

  • Character functions, numeric functions and converting variable type
  • Use functions in data transformation

Deep Dive into SAS Procedures, Functions and Statements

  • Find Function
  • Scan Function
  • MERGE Statement
  • BY Statement
  • Joins
  • Procedures Vs Function
  • Where Vs If
  • What is Missover
  • NMISS
  • CMISS

PROC SQL

  • SELECT statement
  • Sorting of Data
  • CASE expression
  • Other SELECT statement clauses
  • JOINS and UNIONS

Using SAS Macros

  • Benefits of SAS Macros
  • Macro Variables
  • Macro Code Constituents and Macro Step
  • Positional Parameters to Macros

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Strings Manipulation and Arrays in SAS"

Leave a Message

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