SAS has another set of very important set of functions, which are used in preparation of data. These functions are numeric functions. Below is a table for some of the numeric function.
Numeric Functions:
Function | Application |
Int (Variable_name) | Give integer part of variable |
Round(Variable_name, rounding off value) | An rounded value of variable is returned |
missing(Variable_name) | Return a '1' in case of variable has no value |
n( of var1-var2) | Return no. of observations |
n( variable1_name, variable2_name, ...) | Return count of non missing variable in same observations |
min( of var1-varn) | return min. of variable values |
max( of var1-varn) | return max. of variable values |
mean(of var1-varn) | return mean of variable values |
nmiss(of var1-varn) | return count of missing of variable values |
largest( n, of var1-var10) | return nth order largest value like for n =2 second largest, n =3 third largest |
abs(Variable_name) | Will return absolute value |
sqrt(variable_name) | Will return square root of variable value |
exp(variable_name) | Will return exponential |
Lagn(variable) | Will return nth lagged value of variable |
Ranuni (0) | Generates a random value between 0 & 1, which is based on unform distribution |
diffn(variable_name) | Will return a difference of current value & nth lagged value for the variable |
Now lets consider an example of a sales data of a grocery store as mentioned below.
item_id Cost MRP Disc.
GR_012 32.05 41.54 5.05%
GR_121 29.45 32.45 6.97%
CP_312 12.21 15.65 7.23%
DN_121 6.43 7.12 1.05%
Now shopkeeper wants to calculate profit margin if he sells item on MRP & gives discount mentioned in the data. We will have to use a combination of character & numeric variable in order to achieve client's requirement.
Let's first bring this data into SAS.
DATA ITEM_DATA;
INPUT ITEM_ID$ COST MRP DISC$;
DATALINES;
GR_012 32.05 41.54 5.05%
GR_121 29.45 32.45 6.97%
CP_312 12.21 15.65 7.23%
DN_121 6.43 7.12 1.05%
;
RUN;
DATA ITEM_DATA1;
SET ITEM_DATA (RENAME=(DISC = DISCOUNT));
DISC = INPUT(COMPRESS(DISCOUNT,'%'),8.);
SELL_PRICE = (1- DISC/100)*MRP;
MARGIN = ROUND((SELL_PRICE - COST)*100/SELL_PRICE, 0.01);
MARGIN_IN_PERCENT = CAT(PUT(MARGIN, 5.2), '%');
DROP DISCOUNT;
RUN;
There are so many things happening here in this code. I will explain all of them, one at a time.
So as i said earlier also, SAS gives user a method to change a character variable into a numeric variable. Here we have discounts as character datatype. In order to calculate Selling price we have to convert discount into a numeric variable. An input function can be used to do so. We have used rename option to recover original name of the variable. An input statement has a character variable passed into first argument & then a format length modifier, which will tell SAS, in what format do we need this value. Here 8. is a numeric format having length of 8 bytes. We have also used a compress function to remove '%' sign from the discount variable.
Now once we have disc in numeric format we can make calculations for Margin, Since most of the time we need a percent upto 2 decimal value we have used a Round function to convert a value into 2 decimal format.
To understand a numeric to character conversion, we have used margin variable & used a PUT function which will convert Margin_in_percent as character value. For future calculation purpose we can use Margin, which is still a numeric value.
No comments:
Post a Comment