Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SAS functionality


May 26, 2021 SAS


Table of contents


SAS has a variety of built-in functions to help analyze and process data. T hese functions are used as part of the DATA statement. T hey take the data variable as an argument and store the results in another variable. D epending on the type of function, the number of parameters required may vary. S ome functions accept zero arguments, while others accept a fixed number of variables. T he following is a list of the types of features provided by SAS.

Syntactic

The general syntax for using functions in SAS is as follows.

FUNCTIONNAME(argument1, argument2...argument)

The arguments here can be constants, variables, expressions, or another function.

Functional classification

Depending on their usage, features in SAS are classified as follows.

  • Mathematical
  • Date and time
  • Character
  • give up
  • Miscellaneous

Mathematical functions

These are functions that apply some mathematical calculations to variable values.

Example

The following SAS program shows the use of some important mathematical functions.

data Math_functions;
v1=21; v2=42; v3=13; v4=10; v5=29;

/* Get Maximum value */
max_val = MAX(v1,v2,v3,v4,v5);

/* Get Minimum value */
min_val = MIN (v1,v2,v3,v4,v5);

/* Get Median value */
med_val = MEDIAN (v1,v2,v3,v4,v5);

/* Get a random number */
rand_val = RANUNI(0);

/* Get Square root of sum of the values */
SR_val= SQRT(sum(v1,v2,v3,v4,v5));

proc print data = Math_functions noobs;
run;

When the above code runs, we get the following output:

SAS functionality

Date and time functions

These are functions that handle date and time values.

Example

The following SAS program shows the functions that use the date and time.

data date_functions;
INPUT @1 date1 date9. @11 date2 date9.;
format date1 date9.  date2 date9.;

/* Get the interval between the dates in years*/
Years_ = INTCK('YEAR',date1,date2);

/* Get the interval between the dates in months*/
months_ = INTCK('MONTH',date1,date2);

/* Get the week day from the date*/
weekday_ =  WEEKDAY(date1);

/* Get Today's date in SAS date format */
today_ = TODAY();

/* Get current time in SAS time format */
time_ = time();
DATALINES;
21OCT2000 16AUG1998
01MAR2009 11JUL2012
;
proc print data = date_functions noobs;
run;

When we run the code above, we get the following output:

SAS functionality

Character function

These are features that handle character or text values.

Example

The following SAS program shows the character functions used.

data character_functions;

/* Convert the string into lower case */
lowcse_ = LOWCASE('HELLO');
  
/* Convert the string into upper case */
upcase_ = UPCASE('hello');
  
/* Reverse the string */
reverse_ = REVERSE('Hello');
  
/* Return the nth word */
nth_letter_ = SCAN('Learn SAS Now',2);
run;

proc print data = character_functions noobs;
run;

When we run the code above, we get the following output:

SAS functionality

Truncation function

These are the features used to truncated numeric values.

Example

The following SAS program explains how to use truncation.

data trunc_functions;

/* Nearest greatest integer */
ceil_ = CEIL(11.85);
  
/* Nearest greatest integer */
floor_ = FLOOR(11.85);
  
/* Integer portion of a number */
int_ = INT(32.41);
  
/* Round off to nearest value */
round_ = ROUND(5621.78);
run;

proc print data = trunc_functions noobs;
run;

When we run the code above, we get the following output:

SAS functionality

Other features

Example

The following SAS program shows the accessibility features used.

data misc_functions;

/* Nearest greatest integer */
state2=zipstate('01040');
 
/* Amortization calculation */
payment=mort(50000, . , .10/12,30*12);

proc print data = misc_functions noobs;
run;

When we run the code above, we get the following output:

SAS functionality