How can I use the MONOTONIC function in SAS and what are some examples of its usage? 2

How can I use the MONOTONIC function in SAS and what are some examples of its usage?

The MONOTONIC function in SAS is a powerful tool that allows users to create a unique sequential number for each observation in a data set. This function can be used in various ways, such as creating an index variable or sorting data in a specific order. By using the MONOTONIC function, users can easily keep track of the order in which their data was entered or sorted. For example, the function can be used to create a unique ID number for each customer in a sales dataset, making it easier to track transactions and analyze customer behavior. Additionally, the function can be used to create a sequential date or time stamp for data collected at different intervals. Overall, the MONOTONIC function in SAS is a useful tool for organizing and analyzing data in a precise and structured manner.

Use MONOTONIC Function in SAS (With Examples)


You can use the MONOTONIC() function in SAS to generate row numbers for a dataset.

Here are two common ways to use this function in practice:

Method 1: Use MONOTONIC() to Create Column of Row Numbers

/*create column called row_ID that contains row numbers*/
proc sql;
    select team, monotonic() as row_ID
    from my_data;
quit;

Method 2: Use MONOTONIC() to Filter Dataset by Row Numbers

/*filter where row number is less than 5*/
proc sql;
    select *
    from my_data
    where monotonic() < 5;
quit;

The following examples show how to use each method in practice with the following dataset in SAS:

/*create dataset*/
data my_data;
    input team $ points assists;
    datalines;
Cavs 12 5
Cavs 14 7
Warriors 15 9
Hawks 18 9
Mavs 31 7
Mavs 32 5
Mavs 35 3
Celtics 36 9
Celtics 40 7
;
run;

/*view dataset*/
proc printdata=my_data;

Example 1: Use MONOTONIC() to Create Column of Row Numbers

The following code shows how to use the MONOTONIC() function to create a new column called row_ID that contains the row number (starting from 1) for each observation in the dataset:

/*create column called row_ID that contains row numbers*/
proc sql;
    select team, monotonic() as row_ID
    from my_data;
quit;

Notice that a new column has been created called row_ID that contains the row number for each observation in the dataset, ranging from 1 to 9.

Example 2: Use MONOTONIC() to Filter Dataset by Row Numbers

The following code shows how to use the MONOTONIC() function to filter a dataset where the row number is less than 5:

/*filter where row number is less than 5*/
proc sql;
    select *
    from my_data
    where monotonic() < 5;
quit;

Notice that only the first four rows from the dataset are shown since we used the MONOTONIC() function to specify that the row number must be less than 5.

The following tutorials explain how to perform other common tasks in SAS:

Cite this article

stats writer (2024). How can I use the MONOTONIC function in SAS and what are some examples of its usage?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-monotonic-function-in-sas-and-what-are-some-examples-of-its-usage/

stats writer. "How can I use the MONOTONIC function in SAS and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-monotonic-function-in-sas-and-what-are-some-examples-of-its-usage/.

stats writer. "How can I use the MONOTONIC function in SAS and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-monotonic-function-in-sas-and-what-are-some-examples-of-its-usage/.

stats writer (2024) 'How can I use the MONOTONIC function in SAS and what are some examples of its usage?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-monotonic-function-in-sas-and-what-are-some-examples-of-its-usage/.

[1] stats writer, "How can I use the MONOTONIC function in SAS and what are some examples of its usage?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use the MONOTONIC function in SAS and what are some examples of its usage?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top