Functions in SAS

Ratings:
(4)
Views:0
Banner-Img
  • Share this blog:

Functions:

It requires some arguments(variables or data values) and do some action and generate some result. These result will be stored in another variable. There are 3 types of functions. They are

  1. Arithmetic functions
  2. String functions
  3. Date and time functions

 

Arithmetic functions:

      a)  Int function: Using int function, we can get integer values from the variables. Syntax: Int(variable/data value)         b)  Round function: Using round function we can round up the value nearest integers or nearest decimal places.         c)  Absolute(abs) function: This convert all data values into positive format.          d) Mod function: It returns reminder. Ex: Data demo; Input pid age weight; Cards; 101  24  78.55 102  23  56.46 103  34  -67.33 104  35  76.56 105  23  56.44 106  23  67.74; Data demo 1; Set demo; Wkg = int (weight); Wrkg = round(weight); Wrdkg = round(weight, .1); Wabs = abs(weight); Run; Proc print data = demo1; Run; Ex: Data one; X = mod(34,3); Run; Proc print; Run;

Desired to gain proficiency on SAS? Explore the blog post on SAS Training to become a pro in SAS.

Semantic error:

If we send wrong number of argument for functions. In this case we will get one type of execution error. This execution error is called semantic error.   Note: If we want to create a subset of data based on new variable creations. In this case, we will use if statement instead of where statement. Ex: Data demo2; Set demo; i= mod(-n-, 2); if i=1; run; proc print data =demo2; run; Ex: Data demo2; Set demo; i= mod(-n-, 2); if i=1; run; proc print data =demo2; where I =0; run;   Note: We can create subset of data using if and where statement. If we use the if statement, pdv checks data error in all observation and next check the condition. This is very lengthy process. If we use the where statement, pdv 1st of all check the condition after that brings into pdv and checks the data error.  

Sum function:

It can be used to do row wise sum analysis or row wise sums. Note: Using procedure block we can do row wise and column wise analysis. Using data set block we can do row wise sum analysis only.  

Propagation error (or)execution error:

If we do any arithmetic operations using with arithmetic operator then if we get any missing value in raw data, the result is also missing. This is called as propagation error, to overcome this problem use with functions. Ex: Data emp; Input eid   jsale   fsale  msale; Cards; 100  200  300  230 101  230    .     340 102  45 0  500  230 ; Data emp1; Set emp; Tot sale = sum (jsale ,  fsale,  msale); Avgsal = mean (jsale ,  fsale,  msale); Max sal = max(jsale ,  fsale,  msale); Min sal = min (jsale ,  fsale,  msale); Run; Proc print data =emp1; Run;  

Keep , drop, rename statement:

These we can use only in data set block. Ex: Data set input Gid $ drug $ week sub; Cards; G100  col5mg  3  120 G200  col5mg  3  130 G300  col5mg  3  140 G100  col10mg  6  120 ; Data medi1; Set trt; Keep Gid drug sub; Run; Data medi2; Set trt; Drop week; Run; Proc print data =medi1; Run; If we want to change the Gid, then we write after drop statement, rename gid = Groupid; Data medi2;set trt; Drop week; Rename gid =groupid; Run; Proc print data =medi2; Run;  

Special automatic variables:

-Numeric- -character- _Numeric_ _Character_   -Numeric-: Hyphen numeric hyphen, it indicates part of numerical based on range.    -Character-: Hyphen character hyphen, it indicates part of character variables based on range.      _Numeric_: Underscore numeric underscore indicate all numeric variable   _Character_:     Underscore character underscore indicate all character variable /* To read all numeric variables*/ Ex: Data medi 3; Set trt; Keep _ numeric_; Run; /* To read all character variables*/ Ex: Data medi 4; Set trt; Keep _ character_; Run; /* To read part of the numeric variables*/ Ex: Data one; Input name $ age area $ height weight gender $; Cards; Kiran  56 hyd  5.6  67  male Kumar  34  vij  4.5  56  male ; Data two; set one; Keep area – numeric – gender; Run; Proc print data = two; Run;

 

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox