Saturday, October 21, 2023

SAS Base: A Comprehensive Guide to SAS Base Functions in Programming

Importance of SAS Base Functions in Data Manipulation and Analysis:

SAS Base functions are essential in the data manipulation and analysis for the following reasons:

Data Cleaning: SAS Base functions help in cleaning and preparing raw data by allowing users to identify and rectify inconsistencies, missing values, and outliers.

Data Transformation: These functions facilitate the transformation of data, making it suitable for analysis. Functions like SUBSTR, TRIM, and DATE functions help in shaping data as required.

Descriptive Statistics: SAS Base functions enable the calculation of basic statistics like mean, median, and mode, providing an initial understanding of data distribution.

Conditional Logic: Functions like IF-THEN-ELSE and CASE expressions allow analysts to apply conditional logic, making it easier to filter and categorize data based on specific criteria.

Text and String Manipulation: With functions like UPCASE, LOWCASE, CAT, COMPRESS, and TRANWRD, SAS can handle text data effectively, a crucial component in many analyses.


Section 1: Basic SAS Functions

PRINT:

Basically, PRINT function is used to display your output results in Result/Output window.

There is a simple syntax;

        Proc Print Data = Mylib.Libname;


MEAN, MEDIAN, and MODE:

A basic descriptive statistics like mean, median, mode that will be calculated by using the SAS functions, there is simple syntax for it.

        Mean = MENA(Varname);

        Median = MEDIAN(Varname);

        Mode = MODE(Varname);

Also we can find MIN & MAX occurrences in the dataset by using other SAS functions like;

        Min = MIN(Varname);

        Max = MAX(Varname);


SUM and COUNT:

Generally, the SUM and COUNT functions are used to determine the total of a particular variable and identify the frequency distribution of the categorical variable.

SAS syntax:

        Total = SUM(Varname);

        Frequencies = COUNT(Cat_Varname);


Section 2: Data Transformation Functions

SUBSTR:

SUBSTR is SAS function, it can be used to extract the substring from the categorical variable. It allows you to specify the starting position and length of substring which you want of extract from main string.

SAS Syntax:

        Substring = SUBSTR(String, Starting_Position, Length);

Example:

        Data sas;
        sas1 = "I have completed SAS Base certification";
        sas3 = SUBSTR(sas1, 18, 3); /* In result "SAS" */
        Run;

TRIM and STRIP:

The TRIM function in SAS is used to remove trailing spaces from character string.

SAS Syntax:

        Trim = TRIM(Cat_Varname);

Example:

        Data Example;

        X = " Arjun ";

        Trim = TRIM(X); /* In result " Arjun" */

        Run;

But STRIP function in SAS is used to remove the both leading and trailing spaces from character string.

SAS Syntax:

        Strip = STRIP(Cat_Varname);

Example:

        Data Example;

        X = " Arjun ";

        Strip = STRIP(X); /* In result "Arjun" */

        Run;


DATE Functions:

INTNX:

INTNX function is used to increment or decrement a date by specified interval. its particularly useful for generating future and past dates based on a reference date.

SAS Syntax:

        INTNX(Interval, Date, Increment);

Example:

        Data example;

        Today = '16OCT2023'd;

        Next_month = INTNX('MONTH', Today, 1);

        Run;

In the above example, "INTNX" function is used to calculate the

date of one month after given reference the date.

TODAY:

The "Today" function is used to display current date in SAS format.

SAS Syntax:

        Today = TODAY();

DATEPART:

The "DATEPART" function is used to extract the date part from a SAS datetime value.

SAS Syntax:

Datepart = DATEPART(Datetime);

Example: 

Data Example;

DateTime = '19OCT2023:02:30:00'dt;

Datepart = DATEPART(DateTime);

Run;


Section 3: Conditional Functions

IF-THEN-ELSE:

If you want to extract the subset from the dataset by a particular conditions then "IF-THEN-ELSE" conditional function is useful.

SAS Syntax:

        IF Codition1 then expression1;

        ELSE Expresson2;

        Run;

Example:

        Data Example;

        Set Dataset;

        IF Age > 18 then Status = "Adult";

        ELSE Status = "Younger";

        Run;

In this example, if the condition "Age > 18" is true, the "Status" variable will be assigned the

value 'Adult'; otherwise, it will be assigned 'Younger'.


CASE Expressions:

The "CASE" expression in SAS is used to perform multiple conditions at time on the dataset and create new variables.

SAS Syntax:

        CASE

            WHEN Condition1 THEN Result1

            WHEN Condition1 THEN Result1

            WHEN Condition1 THEN Result1

        END;

Example:

        Data Example;

        Set Dataset;

            CASE

                    WHEN Score > 10 THEN Result = "A"

                    WHEN Score > 20 THEN Result = "B"

                    WHEN Score > 30 THEN Result = "C"

                ELSE "D"

            END;

        Run;


Section 4: Text and String Functions

UPCASE, LOWCASE and PROPCASE:

If you want to convert lower case character to upper case then we can use "UPCASE" function and to convert upper case character to lower case by using "LOWCASE" function.

SAS Syntax:

Upcase = UPCASE(Cat_Varname);

Lowcase = LOWCASE(Cat_Varname);

Propercase = PROPCASE(Cat_Varname);

IF you want to convert character string or statement into proper sentence format then we can use "PROPCASE" function in SAS.

Example;

        Data Example;

        String = "How are you";

        Lowercase = LOWCASE(String);/*In result "how are you"*/

        Uppercase = UPCASE(String);/*In result "HOW ARE YOU"*/

        Propercase = PROPCASE(String);/*In result "How Are You"*/

        Run;

Concatenate(CAT):

CAT: Concatenates without spaces or delimiters, doesn't remove spaces, and produces a missing result if any input is missing.

SAS Syntax:

        CAT(String1, String2, .... , String3);

Example:
        Data Example;
        Name = ' John';
        Age = ' 25';
        Result = CAT(name, age); /* result is ' John 25' */
        Run;

CATS: Concatenates without spaces or delimiters, removes spaces, and produces a missing result if any input is missing.

SAS Syntax:

        CATS(String1, String2, .... , String3);

Example:
        Data Example;
        Name = ' John ';
        Age = ' 25';
        Result = CATS(name, age); /* result is 'John25' */
        Run;

CATX: Concatenates with a specified delimiter, removes spaces, and includes the delimiter even if some inputs are missing.

SAS Syntax:

        CATX('Delimiter', String1, String2, .... , String3);

Example:
        Data Example;
        Name = ' John';
        Age = ' 25';
        Gender = ' ';
        Result = CATX(', 'name, age); /* result is 'John, 25, ' */
        Run;

COMPRESS:

In SAS, "COMPRESS" function is used to remove specific characters from the character string. Its allows you to clean, filter and manipulate test data by eliminating unwanted characters.

SAS Syntax:

COMPRESS(source-string, characters-to-remove, modifier);

Here is a list of modifiers in the COMPRESS function in SAS.

a – Remove all alphabetical characters from String.

ak – Keep only alphabets from String.

kd – Keep only numeric values from String.

d – Remove numerical values from String.

i – Remove specified characters (both upper and lower case) from String.

k – Keep the specified characters in the String instead of removing them.

l – Remove lowercase characters from String.

p – Remove punctuation characters from String.

s – Remove spaces from String. This is the default.

u – Remove uppercase characters from String.

Example:

        Data string;

       str="ArjunMane2422@gmail.com*";

        alp1=compress(str,'Arjun');

        alp1=compress(str,'','a');

        alp1=compress(str,'','d');

        alp2=compress(str,'','i');

        alp3=compress(str,'','k');

        alp4=compress(str,'','l');

        alp5=compress(str,'','u');

        run;


TRANWRD:

In SAS, the "TRANWRD" function is used to replace specified substrings within a character string with other substrings.
SAS Syntax:
        Tranword = TRANWRD(String, Search, Replace);
Example:
        Data Example;
        String = "I am Arjun";
        Tranword = TRANWRD(String, "Arjun"," Nikhil");
        Run;

Conclusion:

In summary, SAS Base functions are the building blocks of data analysis in SAS. They are essential for data preparation, cleansing, and initial analysis, and they serve as the foundation upon which more advanced statistical and analytical techniques are built. Understanding and mastering these functions is fundamental for anyone working with SAS for data analysis.

Labels: , , , , , , , , , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home