Numeric Functions :
MEAN
Returns the arithmetic mean (average)
Argument is numeric At least one non-missing argument is required otherwise, the function returns a missing value
Syntax: – MEAN(argument<,argument,…>)
Data ds1;
x1=mean(2,.,.,6);
x2=mean(2,4,5,6);
x3=mean(x1-x2); /*x3=mean(4-4.25)=-0.25/1=-0.25*/
x4=mean(of x1-x2); /*it means x1, x2 means 4,4.25 means 8.25/2=4.125*/
x5=mean(x1,x2);
Run;
Data ds1;
x1=mean(2,.,.,6);
x2=mean(2,4,5,6);
x3=mean(2,4,5,6);
y=mean(3,3,5);
x4=mean(x1-x2);
x5=mean(of x1-x3);
x6=mean(x1,x2);
xa=mean(2,5);
x7=mean(of x:);
x8=mean(of x1–x4);
Run;
MEDIAN
Computes median values.
Syntax: – MEDIAN(value1<, value2, …>)
Data ds2;
x=median(2,4,1,3);
y=median(5,8,0,3,4);
z=median(5,.,0,.,4);
Run;
Difference between MEAN & MEDIAN
Mean will give average of numeric values
Ex:- x=mean(70,60,80,75,90)
x=70+60+80+75+90/5
x=375/5=75
In MEDIAN data will arrange from lowest to highest
in that data middle no is MEDIAN value
60,70,75,80,90
75 is mid value which is median value
Ex:- x=median(2,4,1,3);
in above example mid value is 4,1
it means 4+1=5
median value is 5/2=2.5;
MIN
Returns the smallest value
Syntax: – MIN(argument,argument,…)
Data ds3;
x1=min(7,4);
x2=min(2,.,6);
x3=min(2,-3,1,-1);
x4=min(0,4);
x6=min(of x1-x3);
x7=min(x1,x3);
Run;
MAX
Returns the largest value
Syntax:-MAX(argument,argument,…)
Data ds4;
x=max(8,3);
x1=max(2,6,.);
x2=max(2.-3,1,-1);
x3=max(3,.,-3);
x4=max(.,.,.);
x5=max(of x1-x3);
Run;
Argument is numeric. At least two arguments are required. The argument list may consist of a variable list, which is preceded by OF.
The MAX function returns a missing value (.) only if all arguments are missing.
RANGE
Returns the range of values
Syntax:- RANGE(argument,argument,…)
Argument is numeric At least one non missing argument is required. Otherwise, the function returns a missing value. The argument list can consist of a variable list, which is preceded by OF.
The RANGE function returns the difference between the largest and the smallest of the non missing arguments.
Data ds5;
x1=range(.,.);
x2=range(-2,6,3);
x3=range(2,6,3,.);
x4=range(1,6,3,1);
x5=range(of x1-x3);
run;
SUM
Returns the sum of the non missing arguments
Syntax:-SUM(argument,argument, …)
Argument is numeric If all the arguments have missing values, the result is a missing value. The argument list can consist of a variable list, which is preceded by OF
Data ds6a;
x1=sum(4,9,3,8);
x2=sum(4,9,3,8,.);
x3=sum(of x1-x2);
Run;
Data ds6b;
x1=5;
x2=6;
x3=4;
x4=9;
y1=34;
y2=12;
y3=74;
y4=39;
result=sum(of x1-x4, of y1-y5);
Run;
Data ds6c;
x1=55;
x2=35;
x3=6;
x4=sum(of x1-x3, 5);
Run;
Data ds6d;
x1=7;
x2=7;
x5=sum(x1-x2);
Run;
Data ds6e;
y1=20;
y2=30;
x6=sum(of y:);
Run;
/*Sum Statement*/
Adds the result of an expression to an accumulator variable
Syntax:-variable+expression;
Data ds6;
x1=sum(4+9+3+8);
x2=sum(4+.+9+3+8+.);
Run;
SUM Function returns the sum of non missing values
Ex:- x2=sum(4,.,9,3,8,.); it gives value 24
SUM Statement Adds the value into variable with non missing values
its won’t consider missing values.
if missing value are there value is .
ex:- x2=sum(4+.+9+3+8+.); it gives value .
VAR
Returns the variance
Syntax:-VAR(argument,argument, …)
argument is numeric. At least two non missing arguments are required. Otherwise, the function returns a missing value. The argument list can consist of a variable list, which is preceded by OF.
Data ds13;
x1=Var(4,2,3.5,6);
x2=Var(4,6,.);
x3=Var(of x1-x2);
Run;
SQRT
Returns the square root of a value.
Syntax :-SQRT(argument)
argument is numeric and must be nonnegative
Data ds14;
x1=sqrt(36);
x2=sqrt(25);
x3=sqrt(4.4);
x4=sqrt(-49);
Run;
NMISS
Returns the number of missing values
Syntax :-NMISS(argument<,…argument-n>)
argument is numeric. At least one argument is required. The argument list may consist of a variable list, which is preceded by OF.
Data ds15;
x1=nmiss(1,0,.,2,5,.);
x2=nmiss(1,0);
x3=nmiss(of x1-x2); /*x1=2 x2=0 so 2,0 it gives 0*/
Run;
N
Returns the number of non missing values
Syntax:-N(argument<,…argument-n>)
argument is numeric. At least one argument is required. The argument list may consist of a variable list, which is preceded by OF.
Data ds16;
X1=n(1,0,.,2,5,.);
X2=n(1,0);
X3=n(of x1-x2);
Run;
CMISS
Counts the number of missing arguments.
Syntax: – CMISS (argument-1 <, argument-2,…>)
Data ds;
Set sashelp.class;
If name=’Barbara’ then height=.;
If name=’Philip’ then weight=.;
Run;
Data ds2;
Set ds;
If cmiss(of name–weight);
RUN;
Data ds3 ds4;
Set ds;
If cmiss (of name–weight) then output ds2;
Else output ds3;
Run;
COALESCE
Returns the first non-missing value from a list of numeric arguments.
Syntax: – COALESCE(argument-1<…, argument-n>)
Data Ds;
X = coalesce(42, .);
Y = coalesce(., 7, ., ., 42);
Z = coalesce(.5,10,.,12);
Run;
COALESCEC
Returns the first non-missing value from a list of character arguments.
Syntax: – COALESCE(argument-1<…, argument-n>)
Data ds;
X = coalescec(‘ ‘, ‘hello’);
Y = coalescec (‘ ‘, ‘goodbye’, ‘hello’);
Z = coalescec (‘sas’, ‘stansys’, ‘krishna’);
Run;
LAG
Returns values from a queue.
Syntax:- LAG<n>(argument)
Data lg1;
input x @@;
a=lag1(x);
b=lag2(x);
c=lag3(x);
d=lag(x);
datalines;
1 2 3 4 5 6
;
Run;
Data lg2;
input x @@;
y=lag1(x+10);
z=lag2(x);
datalines;
1 2 3 4 5 6
;
Run;
********** LOCF (Last Observations Carry Forward) ************
DATA TEST;
INPUT PAT VISIT LABSTD;
CARDS;
101 1 0.1
101 2 0.3
101 3 .
101 5 0.1
101 6 0.9
102 1 0.7
102 2 0.3
102 3 .
102 5 0.4
102 6 0.9
;
Run;
Data test1;
Set test;
LABSTD1=lag(LABSTD);
If LABSTD=. then LABSTD=LABSTD1;
Run;
DATA TEST2;
INPUT PAT VISIT LABSTD;
CARDS;
101 1 0.1
101 2 0.3
101 3 .
101 5 0.1
101 6 0.9
102 1 .
102 2 0.3
102 3 .
102 5 0.4
102 6 0.9
;
RUN;
Data test3;
Set test2;
LABSTD1=lag(LABSTD);
Run;
Proc sort data=test3;
By PAT;
Run;
Way#1
Data test4(drop=LABSTD1);
Set test3;
By pat;
If first.PAT and LABSTD=. then LABSTD=.;
Else if LABSTD=. then LABSTD=LABSTD1;
Run;
Way#2
Data test3(drop=x y);
Set test2;
if PAT=101 then x=lag(labstd);
if PAT=102 then y=lag(labstd);
if PAT=101 and labstd=. then labstd=x;
if PAT=102 and labstd=. then labstd=y;
Run;
How can i find out difference between value1 values with value1 values
like 40-20, 60-40, 80-60 etc?
Example:-
Data ds;
Infile datalines;
Input id value1;
Datalines;
001 20
002 40
003 60
004 80
005 100
;
Run;
Data ds2(drop=value2 value4);
Set ds;
value2=lag(value1);
value3=sum(value1,-value2);
value4=value1-value2;
Run;
ANY DIGIT
Searches a character string for a digit and returns the first position at which it is found
Syntax:- ANYDIGIT(string <,start>)
DATA SEARCH_NUM;
INPUT STRING $60.;
dg = ANYDIGIT(STRING);
DATALINES;
This line has a 56 in it
two numbers 123 and 456 in this line
No digits here
;
Run;
ANY SPACE
Searches a character string for space returns the first position at which it is found
Syntax:- ANYSPACE(string <,start>)
DATA SEARCH_SPACE;
INPUT STRING $60.;
SP= ANYSPACE(STRING);
DATALINES;
This line has a 56 in it
two numbers 123 and 456 in this line
No digits here
;
Run;
How can you separate numeric values from alpha numeric value
DATA EN;
INPUT STRING $60.;
START = ANYDIGIT(STRING);
END = ANYSPACE(STRING,START);
IF START NE 0 THEN NUM = INPUT(SUBSTR(STRING,START,END-START),9.);
DATALINES;
This line has a 56 in it
two numbers 123 and 456 in this line
No digits here
;
Run;
Decimal Handling Functions
CEIL
Returns integer that is greater than or equal to the argument, fuzzed to avoid unexpected floating-point results
Syntax :-CEIL (argument)
Data ds7;
var1=2.1;
a=ceil(var1);
Run;
Data ds7;
b=ceil(-2.4);
c=ceil(1+1.e-11);
d=ceil(-1+1.e-11);
e=ceil(1+1.e-13);
f=ceil(223.456);
g=ceil(763);
h=ceil(-223.456);
Run;
FLOOR
Returns integer that is less than or equal to the argument, fuzzed to avoid unexpected floating-point results.
Syntax :-FLOOR (argument)
Data ds8;
var1=2.1;
a=floor(var1);
Run;
Data ds8;
b=floor(-2.4);
c=floor(1+1.e-11);
d=floor(-1+1.e-11);
e=floor(1+1.e-13);
f=floor(223.456);
g=floor(763);
h=floor(-223.456);
Run;
ABS
Returns the absolute value
Syntax :-ABS (argument)
If value is negative it converts into positive.
Data ds9;
x1=abs(2.4);
x2=abs(-3);
Run;
INT
Returns the integer value, fuzzed to avoid unexpected floating-point results.
Syntax:-INT(argument)
Data ds10;
x1=INT(2.4);
x2=INT(2.5);
x3=INT(2.8);
X4=INT(-2.4);
Run;
MOD
Returns the remainder from the division of the first argument by the second argument, fuzzed to avoid most unexpected floating-point results.
Syntax:- MOD (argument-1, argument-2)
Data ds11;
X1=MOD(10,3);
Run;
Data ds;
A=123456;
X=INT(A/1000);
Y=MOD(A,1000);
Z=MOD(INT(A/100),100);
Run;
How to select odd numbers, even numbers and prime numbers from SAS dataset.
Data prime;
Do i=1 to 1000;
Output;
End;
Stop;
Run;
Data add even;
Set prime;
If mod(i,2)=0 then output even;
else output add;
/*If mod(i,2)=0 then output even;*/
/*If mod(i,2)=1 then output add;*/
Run;
Data prime_num;
Set prime;
If mod(i,1)= 0 and mod(i,i) = 0 and mod(i,2) ^= 0
and mod(i,3) ^= 0 and mod(i,5)^=0 and mod(i,7)^=0 then
pirime_numbers=i;
If i=3 or i=2 or i=5 or i=7 then pirime_numbers=i;
If not missing(pirime_numbers);
Put pirime_numbers;
Drop i;
Run;
ROUND
Rounds the first argument to the nearest multiple of the second argument, or to the nearest integer when the second argument is omitted.
Syntax:- ROUND (argument <,rounding-unit>)
Data ds12;
x1=ROUND(2.4);
x2=ROUND(2.5);
x3=ROUND(2.8);
X4=ROUND(-2.4);
X5=ROUND(-2.5);
Run;
Data rounding;
d1 = round(1234.56789,100) ;
d2 = round(1234.56789,10) ;
d3 = round(1234.56789,1) ;
d4 = round(1234.56789,.1) ;
d5 = round(1234.56789,.01) ;
d6 = round(1234.56789,.001) ;
d7 = round(1234.56789,.0001) ;
d8 = round(1234.56789,.00001) ;
d9 = round(1234.56789,.1111) ;
/* d10 has too many decimal places in the value for rounding-unit.*/
d10 = round(1234.56789,.11111) ;
Data type Converting Functions
INPUT
Converts data values from character to numeric data type with help of Informat
Syntax:- Input(variable, informat);
Example:-
Data ds1;
Infile datalines;
Input id$ name$ sal;
Datalines;
001 abc 60000
002 def 45000
003 xyz 50000
;
Run;
Data cn /*(drop=id rename=(id1=id))*/;
Set ds1;
id1=input(id, best.);
Run;
PUT
Converts data values from numeric to character data type with help of Format
Syntax:- put(variable, format);
Example:-
Data ds2;
Infile datalines;
Input id name$ sal;
Datalines;
001 abc 60000
002 def 45000
003 xyz 50000
;
Run;
Data nc/*(drop=id rename=(id1=id))*/;
Set ds2;
id1=put(id, $8.);
Run;