0% found this document useful (0 votes)
8 views33 pages

All 37 Functions

The document provides an overview of the 'substr' and 'scan' functions in SAS, detailing their usage for extracting and manipulating text strings. It explains how to use these functions to create new variables, replace values, and handle errors related to invalid arguments. Additionally, it covers the 'compress' and 'compbl' functions for managing whitespace in strings.

Uploaded by

Piyush Ojha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views33 pages

All 37 Functions

The document provides an overview of the 'substr' and 'scan' functions in SAS, detailing their usage for extracting and manipulating text strings. It explains how to use these functions to create new variables, replace values, and handle errors related to invalid arguments. Additionally, it covers the 'compress' and 'compbl' functions for managing whitespace in strings.

Uploaded by

Piyush Ojha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

1.

SUBSTR
The substr function extracts a text from the character variable, and the length of the new var is
same as that of the string, it takes 3 arguments string name, start position and length.

* If the substr creates a new variable then the length of


the new variable would be same as that of parent variable;

data a;
set sasuser.admit;
a= substr(actlevel,2,2);
run;

2. Substr on left side


* The substr function extracts a text from the character
The SAS System 14:52 Wednesday,
variable, it could also be used to replace the values, when
January 7, 2009 1
it is on the left side it assigns
the new values, suppose you need to change the first two Obs
values from 91 to 00, you can assign by putting it to left x
side;
1
(00) 9999265789
data a;
x='(91) 9999265789';
substr(x,2,2)='00';
run;

proc print data=a;


run;

2a. Substr left side condition


* The substr function extracts a text from the character
variable, it could also be used to replace the values, when
it is on the left side it assigns
the new values, suppose you need to change the first two
values from 91 to 00, you can assign by putting it to left
side. it is similar to the tranwrd and translate funcntion;

*You can use the substr function conditionally changing


the values;

*Let’s say the aim is just to replace the HI to HO, so


keeping LOW and MOD untouched;

data a;
set sasuser.admit;
if substr(actlevel,1,2)='HI' then do;
substr(actlevel,1,2)='HO';
end;
run;

3. Substr counts blanks too The SAS System 14:52 Wednesday, January 7,
* The substr function extracts a text 2009 3
from the character variable, it counts
blanks too, trying to get two chars would Obs x phone
return just one as blank is counted;
1 (91) 9999265789 9

data a;
x='(91) 9999265789';
phone=substr(x,5,2);
run;

proc print data=a;


run;

4. Substr no third argument The SAS System 14:52 Wednesday, January 7,


* The substr function extracts a text 2009 3
from the character variable, and if no
third argument then it would select the Obs x phone
whole string from that point;
1 (91) 9999265789 9999265789

data a;
x='(91) 9999265789';
phone=substr(x,6);
run;

proc print data=a;


run;

5. Substr n value gt string length 55 data a;


* The substr function extracts a text 56 x='(91) 9999265789';
from the character variable, if n value 57 phone=substr(x,6,11);
is gt string length then sas throws an 58 run;
error;
NOTE: Invalid third argument to function SUBSTR
at line 57 column 7.
data a; x=(91) 9999265789 phone=9999265789 _ERROR_=1
x='(91) 9999265789'; _N_=1
phone=substr(x,6,11); NOTE: The data set WORK.A has 1 observations
run; and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.06 seconds
cpu time 0.03 seconds

6. Substr n do not take negative values 81 data a;


* The substr function extracts a text 82 x='(91) 9999265789';
from the character variable, if the 83 phone=substr(x,-6,11);
second argument is -ve then error; 84 run;
NOTE: Invalid second argument to function
data a; SUBSTR at line 83 column 7.
x='(91) 9999265789'; x=(91) 9999265789 phone= _ERROR_=1 _N_=1
phone=substr(x,-6,11); NOTE: The data set WORK.A has 1 observations
run; and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds

**3rd Argument is negative then error***;


87 data a;
data a; 88 x='(91) 9999265789';
x='(91) 9999265789'; 89 phone=substr(x,6,-2);
phone=substr(x,6,-2); 90 run;
run; NOTE: Invalid third argument to function SUBSTR
at line 89 column 7.
x=(91) 9999265789 phone=9999265789 _ERROR_=1
_N_=1
NOTE: The data set WORK.A has 1 observations
and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
7. The =: as a substr
**The =: works as a substr for defined
number of bytes, it is just used in the
equality condition;

data a;
set sasuser.admit;
if actlevel =: 'HI';
run;

**You can use the where clause as well;

data a;
set sasuser.admit;
where actlevel =: 'HI';
run;

2. SCAN
The scan function searches for a particular string and puts the value in the target variable, the target variable
length using the scan function is 200 chars, the delimiter is by default is blank

0. Length of new variable


**The length of the new variable created
becomes 200 bytes by default, here lname
would be of length 200;

data x;
set sasuser.admit;
lname=scan(name,2,',');
run;

1. SCAN function The SAS System 14:52


* The scan function searches for a Wednesday, January 7, 2009 5
particular string and puts the value in
the target variable, the target variable Obs x school
length using the scan function is 200
chars, here school is 200 char variable 1 amit ka school tha kv kv
now, the delimiter by default is
blank.....IMP

it also takes 3 arguments first being


string second the nth word and third the
delimiter(by default blank), it divides
string into the chunks divided by
delimiters;

data a;
x='amit ka school tha kv';
school=scan(x,5);
run;

proc print data=a;


run;

2. SCAN function – delimiter The SAS System 14:52


* The scan function searches for a Wednesday, January 7, 2009 8
particular string and puts the value in
the target variable, the target variable Obs x school
length using the scan function is
200 chars, here school is 200 char 1 amit~ka~school~tha~kv kv
variable now, the delimiter by default is
blank, you can use the third argument as
the delimiter;

data a;
x='amit~ka~school~tha~kv';
school=scan(x,5,'~');
run;

proc print data=a;


run;

3.SCAN function – delimiter + blank The SAS


System 14:52 Wednesday, January 7, 2009 9
* if a string has blank and delimiter
then what scan picks; Obs x school name

data a; 1 amit~ ka~ school~ tha~* kv * kv amit


x='amit~ ka~ school~ tha~* kv';
school=scan(x,5,'~');
name=scan(x,1,'~');
run;

proc print data=a;


run;

4.imp SCAN function - delimiter+blank The SAS System 14:52


+dlm as first char Wednesday, January 7, 2009 11

* let’s see if a string has blank and Obs x school


delimiter then what scan picks, even if
the string starts with delimiter, it 1 ~amit~ ka~ school~ tha~* kv amit
actually works good even if first char is
delimiter;

data a;
x='~amit~ ka~ school~ tha~* kv';
school=scan(x,1,'~');
run;

proc print data=a;


run;
5.Leading blanks/ Multiple delimiters The SAS System 14:52 Wednesday, January 7,
2009 12
*If there are leading blanks in a string
they are not part of the value, asked in Obs name lname
the Barclays interview(in other words
multiple occurrence of delimiters are 1 Amit Kumar Singh Singh
considered as one delimiter);

data a;
name=' Amit Kumar Singh ';
lname=scan(name,3); The SAS System 14:52 Wednesday, January 7,
run; 2009 13

proc print data=a; Obs name lname


run;
1 ####Amit####Kumar######Singh Singh
*******************************;
data a;
name='#####Amit######Kumar######Singh ';
lname=scan(name,3,'#');
run;

6.Scan from RIGHT The SAS System 14:52 Wednesday, January 7,


**This is very important thing, consider 2009 14
the below example, the data is not
consistent and you wanna pick the last Obs name marks lname
name;
data x; 1 a b c 20 c
input @1 name $5. @7 marks; 2 k d 15 d
datalines; 3 a 25 a
a b c 20 4 n a b 20 b
k d 15 5 k a 30 a
a 25
n a b 20
k a 30
;
run;

data master;
set x;
lname=scan(name,-1);
run;
proc print data=master; The SAS System 14:52 Wednesday, January 7,
run; 2009 15

********email id example**************; domain_


Obs id name
data email;
length id $25; 1 a,[email protected] n.com
2 [email protected] tt.com
input id$;
3 a@[email protected] cc.com
datalines;
a,[email protected]
[email protected]
a@[email protected]
;
run;

data a;
set email;
domain_name=scan(id,-1,'@');
run;

proc print data=a;


run;
7.Scan with multiple dlm The SAS System 14:52 Wednesday, January 7,
*The scan can read diff dlms if no third 2009 16
argument is provided;
Obs str x y z
data a;
str='a*b/k'; 1 a*b/k a b k
x=scan(str,1);
y=scan(str,2);
z=scan(str,-1);
run;

proc print data=a;


run;
8. when the nth word does not exist The SAS System 14:52 Wednesday, January 7,
* when the nth word does not exist in 2009 18
input string, the output will be blank;
Obs x school
data a;
x='amit~ka~school~tha~kv'; 1 amit~ka~school~tha~kv
school=scan(x,6,'~');
run;

proc print data=a;


run;

3. Compress,compbl,strip
3.a. Compress
Compress squeezes the string and takes the delimiter blank as default and same length as parent string,
removes the leading and trailing blanks +internal blanks if blank is the delimiter which is the default case;

0. compress output length 325


*The length of the compressed variable is 326 proc sql;
same as that of the parent variable; 327 describe table a;
NOTE: SQL table WORK.A was created like:
data a;
name=' c v vfv'; create table WORK.A( bufsize=4096 )
name1=compress(name); (
run; name char(8),
name1 char(8)
proc sql; );
describe table a;
quit; 328 quit;
1. compress The SAS System 14:52 Wednesday, January 7,
*Here compress squeezes the string and 2009 20
takes the delimiter blank as default and
same length as parent string, removes the Obs x y
leading and trailing blanks +internal
blanks if blank is the delimiter which is 1 ab c d abcd
the default case;

data amit;
x='ab c d';
y=compress(x);
run;

proc print data=amit;


run;
2. compress – delimiter The SAS System 14:52 Wednesday, January 7,
*Here compress squeezes the string and 2009 21
takes the delimiter blank as default,
lets say you have delimiter as ',', here Obs x y
the delimiter gets removed only, not the
internal blanks; 1 ab, c, d ab c d

data amit;
x='ab, c, d';
y=compress(x,',');
run;

proc print data=amit;


run;

3. Third argument, 1 dlm at a time The SAS System 14:52 Wednesday, January 7,
* The SAS would not recognize the third 2009 27
argument in a compress function, so
compress take only 2 arguments; Obs x y

data amit; 1 ab,c,~d abc~d


x='ab,c,~d';
y=compress(x,',', '~'); WARNING: In a call to the COMPRESS function or
run; routine, the modifier "~" not valid.
NOTE: The data set WORK.AMIT has 1 observations
and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.12 seconds
* In case you want to delete multiple cpu time 0.04 seconds
delimiters in one go, you need to put all
delimiters in second argument in any
order; The SAS System 14:52 Wednesday, January 7,
2009 28
data amit;
x='ab,c,*&%$ ~d'; Obs x y
y=compress(x,'~$%&* ,');
run; 1 ab,c,*&%$ ~d abcd

proc print data=amit;


run;

3.b. Compbl
The compbl function compress the in between blanks as the name suggest and makes the blank gap to
uniform 1, The LEADING MULTIPLE BLANKS ARE REDUCED TO 1 BLANK ONLY; length of output variable is
same as that of parent variable

* The compbl function compress the in The SAS System 14:52 Wednesday, January 7,
between blanks as the name suggest and 2009 38
makes the blank gap to uniform 1, The
LEADING MULTIPLE BLANKS ARE REDUCED TO 1 Obs x y
BLANK ONLY;
1 a b c d a b c d
data amit;
x=' a b c d
';
y=compbl(x);
run;

proc print data=amit;


run;
* SAS fires error as compbl can have only 500 y=compbl(x,'*');
one argument; ------
72
ERROR 72-185: The COMPBL function call has too
data amit; many arguments.
x='a**b*c*d*e';
y=compbl(x,'*'); 501 run;
run;
NOTE: The SAS System stopped processing this
proc print data=amit; step because of errors.
run; WARNING: The data set WORK.AMIT may be
incomplete. When this step was stopped there
were 0
observations and 2 variables.
WARNING: Data set WORK.AMIT was not replaced
because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds

3.c. Strip
The STRIP function just strips the leading and trailing blank(s) and no effect on internal blanks; length of
output variable is same as that of parent variable.

* It just strips the leading and trailing The SAS System 14:52 Wednesday, January 7,
blank(s) and no effect on internal 2009 40
blanks;
Obs x y
data amit;
x=' x c d '; 1 x c d x c d
y=strip(x);
run;

proc print data=amit;


run;

4.Index, Indexc, Indexw


4.a. Index
The INDEX function searches the position of the excerpt or required string in the source string and if excerpt
is not found then it returns 0.

data _null_; 1 data _null_;


x='ab cd fg hi'; 2 x='ab cd fg hi';
y=index(x,'hi'); 3 y=index(x,'hi');
put y=; 4 put y=;
run; 5 run;

********************or*************; y=11
data _null_; NOTE: DATA statement used (Total process time):
x='ab cd fg hi'; real time 0.33 seconds
y='hi'; cpu time 0.00 second
z=index(x,y);
put z=;
run;
2.Index,trim n compress 19 x='a cc dd';
* The index function can be used 20 y='dd ';
with the trim if the excerpt has 21 z=index(x,trim(y));
trailing blanks, as trim removes the 22 put z=;
trailing blanks; 23 run;

data _null_; z=6


x='a cc dd'; NOTE: DATA statement used (Total process time):
y='dd '; real time 0.00 seconds
z=index(x,trim(y)); cpu time 0.00 seconds
put z=;
run;
*you can use compress too; 38 put z=;
39 run;
data _null_;
x='a cc dd'; z=6
y='dd '; NOTE: DATA statement used (Total process time):
z=index(x,compress(y)); real time 0.00 seconds
put z=; cpu time 0.00 second
run;
3.Conditional processing 42 data x;
43 set sasuser.admit;
* The index function can be used to 44 if index(actlevel,"HI") gt 0 then do;
do conditional processing; 45 output;
46 end;
data x; 47 run;
set sasuser.admit;
if index(actlevel,"HI") gt 0 then NOTE: There were 21 observations read from the data
do; set SASUSER.ADMIT.
output; NOTE: The data set WORK.X has 7 observations and 9
end; variables.
run; NOTE: DATA statement used (Total process time):
real time 0.13 seconds
cpu time 0.01 seconds

I 4.b. Indexc
The INDEXC function searches for the occurrence of the first character of the first excerpt from left to right
and if nothing is found then returns 0, good thing about indexc is that it can take multiple excerpt strings as
input.

*it will return the first occurrence 48 data _null_;


of any string to be searched 49 x='abc def';
irrespective of sequence of 50 y=indexc(x, 'de','c');
arguments; 51
52 put y=;
53 run;
data _null_;
x='abc def'; y=3
y=indexc(x, 'de','c'); NOTE: DATA statement used (Total process time):
real time 0.00 seconds
put y=; cpu time 0.01 seconds
run;
*********************; y=2
data _null_; NOTE: DATA statement used (Total process time):
x='abc def'; real time 0.00 seconds
y=indexc(x,'bc', 'c'); cpu time 0.00 seconds

put y=;
run;

I4.c. Indexw
The indexw searches for words in the source string. The INDEXW function searches source, from left to right,
for the first occurrence of excerpt and returns the position in source of the substring's first character. If the
substring is not found in source, then INDEXW returns a value of 0. If there are multiple occurrences of the
string, then INDEXW returns only the position of the first occurrence.
The substring pattern must begin and end on a word boundary. For INDEXW, word boundaries are delimiters,
the beginning of source, and the end of source. If you use an alternate delimiter, then INDEXW does not
recognize the end of the text as the end data.
INDEXW has the following behaviour when the second argument contains blank spaces or has a length of 0:
 If both source and excerpt contain only blank spaces or have a length of 0, then INDEXW returns a
value of 1.
 If excerpt contains only blank spaces or has a length of 0, and source contains character or numeric
data, then INDEXW returns a value of 0.

* The indexw searches for words in 95 data _null_;


the source string, here indexw 96 x='ab cd abd bd';
searches for a whole word bd and it 97 y=indexw(x,'bd');
identifies word in source string by 98 put y=;
the delimiter blank, so there are 4 99 run;
words in source string ab cd abd and
bd, it does not pick abd while it y=11
pics bd as bd is an individual word; NOTE: DATA statement used (Total process time):
real time 0.00 seconds
data _null_; cpu time 0.00 seconds
x='ab cd abd bd';
y=indexw(x,'bd');
put y=;
run;
*it takes the delimiter as argument 102 data _null_;
also*******************; 103 x='ab~cd~abd~bd';
104 y=indexw(x,'bd','~');
data _null_; 105 put y=;
x='ab~cd~abd~bd'; 106 run;
y=indexw(x,'bd','~');
put y=; y=11
run; NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

5. Left
This function removes the leading blanks and the blanks now moves to right, thus the string gets aligned to
LEFT, the length of the new string is equal to the parent string;.

*here we can see by simple put it 116 data x;


117
does not print leading or trailing 118 a=' cc bb';
blanks so we concatenated it with 119 b=left(a);
120 c='*'||a||'*';
‘*’ to show up the leading and
121 d='*'||b||'*';
trailing blanks. 122
123 put a=;
124 put b=;
data x;
125 put c=;
126 put d=;
a=' cc bb'; 127
128 run;
b=left(a);
c='*'||a||'*';
a=cc bb
d='*'||b||'*';
b=cc bb
c=* cc bb*
put a=;
d=*cc bb *
put b=;
NOTE: The data set WORK.X has 1 observations and 4
put c=;
variables.
put d=;
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
run;
cpu time 0.01 seconds

2.Left and trim 154 c='*'||b||'*';


* This function removes the leading 155 d='*'||trim(b)||'*'
157 put c=;
blanks (L) and aligns the string to
158 put d=;
left+ the trim function removes the 159 run;
trailing blanks so a good combo;
c=*cc bb *
data x; d=*cc bb*
a=' cc bb '; NOTE: The data set WORK.X has 1 observations and 4
b=left(a); variables.
c='*'||b||'*'; NOTE: DATA statement used (Total process time):
d='*'||trim(b)||'*'; real time 0.01 seconds
put c=; cpu time 0.00 seconds
put d=;
run;

6. Right
This function removes the trailing blanks and the blanks now moves to left, thus the string gets aligned to
RIGHT, the length of the new string is equal to the parent string;.

*here we can see by simple put it 162 data x;


164 a=' cc bb ';
does not print leading or trailing 165 b=Right(a);
blanks so we concatenated it with 166 c='*'||a||'*';
167 d='*'||b||'*';
‘*’ to show up the leading and
169 put a=;
trailing blanks. ; 170 put b=;
171 put c=;
172 put d=;
data x;
173 run;
a=' cc bb ';
b=Right(a); a=cc bb
c='*'||a||'*'; b=cc bb
d='*'||b||'*'; c=* cc bb *
d=* cc bb*
put a=; NOTE: The data set WORK.X has 1 observations and 4
put b=; variables.
put c=; NOTE: DATA statement used (Total process time):
put d=; real time 0.01 seconds
run; cpu time 0.01 seconds

7.a. Trim
This function removes the trailing blanks (you will be able to see results Only when you use it with
concatenation).
291 data x;
*here we can see by simple put it 292 a='cc bb ';
does not print leading or trailing 293 b=a||'*';
294 c=trim(a)||'*';
blanks so we concatenated it with 295 put b=;
‘*’ to show up the leading and 296
297
put c=;
run;
trailing blanks.
b=cc bb *
data x; c=cc bb*
a='cc bb '; NOTE: The data set WORK.X has 1 observations and 3
b=a||'*'; variables.
c=trim(a)||'*'; NOTE: DATA statement used (Total process time):
put b=; real time 0.00 seconds
put c=; cpu time 0.00 seconds
run;

7.b. TRIMN
TRIM vs. TRIMN - Both TRIM and TRIMN remove trailing blanks from a character string. The only difference is
how they deal with blank strings. If there is a blank string variable, the TRIM function returns one blank
whereas the TRIMN function returns no blank characters.

data sample;
input string $char14.;
datalines;
Mary Smith /* contains trailing blanks */
John Brown /* contains leading blanks */
Alice Park /* contains leading and trailing blanks */
Tom Wang /* contains leading, trailing and multiple blanks
in between */
/* contains a blank string */
;

data sample;
set sample;
original = '*' || string || '*';
trim = '*' || trim(string) || '*';
trimn = '*' || trimn(string) || '*';
run;

8. TRANWARD
The tranwrd function helps in the replacement of a string in a char variable.

* suppose you want to change in Obs actlevel1


sasuser.admit the values of the
actlevel ; 1 higher
2 higher
data amit; 3 low
length actlevel1 $10; 4 mod
set sasuser.admit; 5 low
actlevel1=tranwrd(lowcase(actlevel),'high', 6 higher
'higher'); 7 mod
run; 8 mod
9 low
proc print data=amit(keep = actlevel1); 10 low
run; 11 higher
12 low
*You can replace the existing variable as 13 mod
well************; 14 higher

data amit;
set sasuser.admit;
actlevel=tranwrd(actlevel,'HI', 'ho');
run;
2.Multiple occurrence The SAS System 00:29 Wednesday, July
* Converting the multiple occurence of a 1, 2009 3
string;
Obs name
data x; 1 hihi
input name$; 2 hi
datalines; 3 cc
highhigh
high
cc
;
run;

data a;
set x;
name=tranwrd(name,'high','hi');
run;

proc print data=a;


run;
*The tranwrd function replaces a particular
text of the variable in the dataset by a
new text,
lets say you want to change the sex to nsex
with M to Male;
data a;
set sasuser.admit;
nsex=tranwrd(sex,'M','Male');
run;

9. TRANSLATE
The translate function changes the string character wise the character in to and from should match, any a in
string would be changed to 1 and so on.

data x; The SAS System 00:29 Wednesday, July 1, 2009


name='amit kumar'; 6
newname=translate(name,'123','ami');
*translate(string,to,from); Obs name newname
run;
1 amit kumar 123t ku21r
proc print data=x;
run;
* The translate function changes the The SAS System 00:29 Wednesday, July 1, 2009
string character wise the character in 7
to and from should match, any a in
string would be changed here a would Obs name newname
be changed to 1 , m to 2 and i to
blank; 1 amit kumar 12 t ku21r

data x;
name='amit kumar';
newname=translate(name,'12','ami');
/*translate(string,to,from)*/
run;

proc print data=x;


run;

**ALSO if from is unbalanced then no The SAS System 00:29 Wednesday, July 1, 2009
issues***********; 8

data x; Obs name newname


name='amit kumar';
newname=translate(name,'123','am'); 1 amit kumar 12it ku21r
/*translate(string,to,from)*/
run;
proc print data=x;
run;

***********Multiple Inputs********** The SAS System 00:29 Wednesday, July 1,


* The inputs are fed in groups of to and 2009 9
from,here a is changed to 1 and m to 2
and k to 3 and u to 4; Obs name

data x; 1 12it 3421r


name='amit kumar';
name=translate(name,'12','am','34','ku');
run;
proc print data=x;
run;

***********Last one prevails********* The SAS System 00:29 Wednesday, July 1,


* If the from characters overlap then the 2009 10
last one prevails here first a points to
1 and second a to 9, so the second a Obs name
prevails;
1 92it k429r
data x;
name='amit kumar';
name=translate(name,'12','am','94','au');
run;
proc print data=x;
run;

10. LOWCASE
It converts the string in to small letters or in lower case.

data amit; The SAS System 00:29 Wednesday, July 1, 2009


x='AMIT';
y=lowcase(x); Obs x y
x=lowcase('NANA');
run; 1 nana amit

proc print data=amit;


run;

11. UPCASE
It converts the string in to capital letters or in upper case.

data amit; The SAS System 00:29 Wednesday, July 1, 2009


x='amit';
y=upcase(x); Obs x y
x=upcase('nana');
run; 1 NANA AMIT

proc print data=amit;


run;

12. PROPCASE
It converts the string in to proper case. First letter of each word in upper case and all other latters in lower case.

data amit; The SAS System 00:29 Wednesday, July 1, 2009


x='AMIT';
y=propcase(x); Obs x y z
z=propcase('nana is a good boy');
run; 1 AMIT Amit Nana Is A Good Boy

proc print data=amit;


run;
13. MEAN
The mean sas function can be used to calculate the mean of the values.

data amit; The SAS System 00:29 Wednesday, July 1,


x=10; 2009
y=20;
mean_age=mean(x,y); Obs x y mean_age
run;
1 10 20 15
proc print data=amit;
run;
*VARIABLE LIST***********; The SAS System 00:29 Wednesday, July 1,
*The same output could be used if we pass the 2009
arguments as variable list;
Obs x1 x2 mean_age
data amit1;
x1=10; 1 10 20 15
x2=20;
mean_age=mean(of x1-x2); /* If you omit the OF
then it would calculate x1-x2 and the mean of
the diff */
run;

proc print data=amit1;


run;

14. QUARTER
The qtr function calculates the Qauarter of the date and returns the value ranging from 1 to 4.

data amit; The SAS System 00:29 Wednesday, July 1, 2009


set sasuser.empdata; Obs HireDate qtrs
qtrs=qtr(hiredate);
run; 1 11MAR1992 1
2 19DEC1983 4
proc print data=amit; 3 12MAR1985 1
var hiredate qtrs; 4 16OCT1989 4
run; 5 19DEC1981 4
6 27APR1991 2
*Doing some subsetting**********;

data amit2;
set sasuser.empdata;
if qtr(hiredate) gt 2;
run;

proc print data=amit2;


run;
*passing date value; The SAS System 00:29 Wednesday, July 1, 2009
data a;
date='16feb2012'd; Obs date qtr
qtr=qtr(date);
run; 1 19039 1

proc print data=a;


run;
15. SUM, SUM OF
The SUM functions gives the sum of 2 or more variables. It also provides the sum of range if variables.

x1=sum(4,9,3,8); 24

x2=sum(4,9,3,8,.); 24 Missing value still sum gets calculated

x1=9;
x2=39;
x3=sum(of x1-x2); 48 Sum for Range

x1=5; x2=6; x3=4; x4=9;


y1=34; y2=12; y3=74; y4=39;
result=sum(of x1-x4, of y1-y5); 183 Range of two variables

x1=55;
x2=35;
x3=6;
x4=sum(of x1-x3, 5); 101 Range and a constant value

x1=7;
x2=7;
x5=sum(x1-x2);
0 As diff gets calculated

y1=20;
y2=30;
x6=sum(of y:); 50 Sum of all values of variable y
*The sum of function can be used to calculate table with the sum of range of
variables;

data a;
sale1=5;
sale2=10;
sale3=15;
sale_sum=sum(of sale1-sale3);
run;

*The second way could be if you want to include all sale variables, use the colon wild
card;

data a;
sale1=5;
sale2=10;
sale3=15;
sale_sum=sum(of sale:);
run;

*The sum of function can be used to calculate table with the sum of range of
variables, here just the sale1-sale4 sum is calculated and variable sale5 is created
with null value;

data a;
sale1=5;
sale2=10;
sale3=15;
sale4=5;
sale_sum=sum(of sale1-sale5);
run;

16. DAY
The day function calculates the day of the date and returns the value ranging from 1 to 30/31.

data amit; The SAS System 00:29 Wednesday, July 1, 2009


set sasuser.empdata;
days=day(hiredate); Obs HireDate days
run;
50 17JAN1994 17
proc print data=amit;
var hiredate days;
run;
**Doing some subsetting**********;

data amit2;
set sasuer.empdata;
if day(hiredate) gt 10;
run;

proc print data=amit2;


run;

17. YEAR
The year function calculates the year from a date value.

data amit; The SAS System 00:29 Wednesday, July 1, 2009


set sasuser.empdata;
yr=year(hiredate); Obs HireDate yr
run;
50 17JAN1994 1994
proc print data=amit;
var hiredate yr;
run;
**Doing some subsetting***;

data amit2;
set sasuser.empdata;
if year(hiredate)=1992;
run;

proc print data=amit2;


run;

18. WEEKDAY
The weekday function calculates the day of the date and returns the value ranging from 1 to 7, 1 being the Sunday
and 2 Monday and so on.

data amit; The SAS System 00:29 Wednesday, July 1, 2009


set sasuser.empdata;
wkday=weekday(hiredate); Obs HireDate wkday
run;
50 17JAN1994 2
proc print data=amit;
var hiredate wkday;
run;
**Doing some subsetting***;

data amit2;
set sasuer.empdata;
if weekday(hiredate) gt 5;
run;

proc print data=amit2;


run;

19. Month
The month function calculates the month of the date and returns the value ranging from 1 to 12.

data amit; The SAS System 00:29 Wednesday, July 1, 2009


set sasuser.empdata;
mnths=month(hiredate); Obs HireDate mnths
run;
50 17JAN1994 1
proc print data=amit;
var hiredate mnths;
run;
**Doing some subsetting***;

data amit2;
set sasuer.empdata;
if month(hiredate) gt 10;
run;

proc print data=amit2;


run;

20. MDY
The mdy function creates a numeric date from the values of the month day and year.

The SAS System 00:29 Wednesday, July 1, 2009


data amit;
input name $ month year day; Obs name month year day bdy
datalines;
amit 10 1981 13 1 amit 10 1981 13 13OCT1981
pre 04 1982 20 2 pre 4 1982 20 20APR1982
;
run;

data amit1;
set amit;
attrib bdy format=date9.;
bdy=mdy(month,day,year);
run;

proc print data=amit1;


run;
**Practical use********; The SAS System 00:29 Wednesday, July 1, 2009
data a;
set sasuser.empdata; cnt_flag_1
format hiredate1 date9.; ----------
m=month(hiredate); 0
D=day(hiredate);
y=year(hiredate);
hiredate1=mdy(m,d,y);
if hiredate ne hiredate1 then flag=1;
else flag=0;
drop m d y;
run;

proc sql;
select count(*) as cnt_flag_1 from a where
flag=1;
quit;

21. Date and Today


The date() or today() function can be used interchangeably and they do not need any arguments.

data amit2; The SAS System 00:29 Wednesday, July 1, 2009


set sasuser.empdata;
format current date9.; Obs current currdate
format currdate date7.;
current=today(); 50 29JUN2017 29JUN17
currdate=date();

run;

proc print data=amit2 (keep=current


currdate);
run;

22. PUT
The PUT function is used to convert the numeric values to the character values for the SAS.

**If we do not use put function THE The SAS System 00:29 Wednesday, July 1, 2009 43
SAS LOG INDICATES THE NUMERIC TO
CHARACTER CONVERSION OF THE AGE******; Obs name age monthsalary nameage

data amit; 1 amit 34 123,45.0 amit / 34


input name $ age monthsalary $ ; 2 na 23 213,45.0 na / 23
datalines;
______________Log___________________
amit 34 123,45.00 435
na 23 213,45.00 436 data amit1;
; 437 set amit;
run; 438 nameage= name ||'/'|| age;
439 run;
data amit1; NOTE: Numeric values have been converted to character
set amit; values at the places given by:
nameage= name ||'/'|| age; (Line):(Column).
run; 438:23
NOTE: There were 2 observations read from the data set
proc print data=amit1; WORK.AMIT.
run; NOTE: The data set WORK.AMIT1 has 2 observations and 4
variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.00 seconds

**USING THE PUT FUNCTION***; The SAS System 00:29 Wednesday, July 1, 2009 44

data amit; Obs name age monthsalary nameage


input name $ age monthsalary $ ;
datalines; 1 amit 34 123,45.0 amit /34
2 na 23 213,45.0 na /23
amit 34 123,45.00
na 23 213,45.00 ______________Log___________________
; 453
run; 454 data amit1;
455 set amit;
data amit1; 456 nameage= name ||'/'|| put(age,2.);
set amit; 457 run;
nameage= name ||'/'|| put(age,2.);
run; NOTE: There were 2 observations read from the data set
WORK.AMIT.
NOTE: The data set WORK.AMIT1 has 2 observations and 4
proc print data=amit1; variables.
run; NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.00 seconds

23. INPUT
The Input function is used to convert the character values to the numeric values for the SAS to perform the
calculations.

* Here salary is been converted from the The SAS System 00:44 Wednesday, July 1, 2009
character values to the numeric values;
Obs name age monthsalary salary yearsalry
data amit;
input name $ age monthsalary $ ; 1 amit 34 123,45.0 12345 148140
datalines; 2 na 23 213,45.0 21345 256140

amit 34 123,45.00
na 23 213,45.00
;
run;

data amit1;
set amit;
salary=input(monthsalary, comma9.2);
yearsalry=salary*12;
run;
proc print data=amit1;
run;

24. CATX
The catx function helps in the concatenating of the character strings and no need of left trim.

The CATX function first copies item-1 to the result, omitting leading and trailing blanks. Then for each subsequent
argument item-i, i=2, ..., n, if item-i contains at least one non-blank character, then CATX appends delimiter and
item-i to the result, omitting leading and trailing blanks from item-i. CATX does not insert the delimiter at the
beginning or end of the result. Blank items do not produce delimiters at the beginning or end of the result, nor do
blank items produce multiple consecutive delimiters.

Length of Returned Variable


In a DATA step, if the CATX function returns a value to a variable that has not previously been assigned a length, then
that variable is given a length of 200 bytes. If the concatenation operator (||) returns a value to a variable that has
not previously been assigned a length, then that variable is given a length that is the sum of the lengths of the values
which are being concatenated..

The catx function creates string of 200 bytes in length;

Function Equivalent Code


CATX(SP, OF X1-X4) TRIM(LEFT(X1))||SP||TRIM(LEFT(X2))||SP||TRIM(LEFT(X3))||SP||TRIM(LEFT(X4))
data a; NOTE: There were 21 observations read from the data
set sasuser.admit; set SASUSER.ADMIT.
x=catx(',',actlevel,sex); NOTE: The data set WORK.A has 21 observations and 10
run; variables.
NOTE: DATA statement used (Total process time):
real time 0.40 seconds
proc sql; cpu time 0.03 seconds
describe table a;
quit;
25
26
27 proc sql;
28 describe table a;
NOTE: SQL table WORK.A was created like:

create table WORK.A( bufsize=16384 )


(
ID char(4),
Name char(14),
Sex char(1),
Age num,
Date num,
Height num,
Weight num,
ActLevel char(4),
Fee num format=7.2,
x char(200)
);

29 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.14 seconds
cpu time 0.03 seconds
data amit; The SAS System 00:44 Wednesday, July 1, 2009
input name $ month year day;
datalines; Obs name month year day
amit 10 1981 13 1 amit 10 1981 13
pre 04 1982 20 2 pre 4 1982 20
;
run;

proc print data=amit;


run; The SAS System 00:44 Wednesday, July 1, 2009

data amit1; Obs name month year day namejoin


set amit;
namejoin=catx('*',name,day,year,month); 1 amit 10 1981 13 amit*13*1981*10
run; 2 pre 4 1982 20 pre*20*1982*4

proc print data=amit1;


run;
*CATX function with a series The SAS System 00:44 Wednesday, July 1, 2009
Lets say you want to concatenate the value
of a variable; Obs sp x1 x2 x3 string
data a;
sp='|'; 1 | a b c a|b|c
x1='a';
x2='b';
x3='c';
string=catx(sp,of x1-x3);
run;
proc print data=a;
run;
*CATX function with a series of vars with The SAS System 00:44 Wednesday, July 1, 2009
colon
*The catx just concatenates the value of Obs x1 x2 x3 string
variables with the colon operator on
variable x; 1 a b c a,b,c
data a;
x1=' a';
x2=' b';
x3='c';
string=catX(',',of x:);
run;
proc print data=a;
run;

25. CAT
The cat just concatenates the value of variables.

The CAT function specifies a constant, variable, or expression, either character or numeric. If item is numeric, then its
value is converted to a character string by using the BESTw. format. In this case, leading blanks are removed and SAS
does not write a note to the log.

Length of Returned Variable


In a DATA step, if the CAT function returns a value to a variable that has not previously been assigned a length, then
that variable is given a length of 200 bytes. If the concatenation operator (||) returns a value to a variable that has
not previously been assigned a length, then that variable is given a length that is the sum of the lengths of the values
which are being concatenated.

The cat function creates string of 200 bytes in length;

Function Equivalent Code


CAT(OF X1-X4) X1||X2||X3||X4
data a; The SAS System 00:44 Wednesday, July 1, 2009
set sasuser.admit; Obs x
x=cat(name,sex); 1 Murray, W M
run; 2 Almers, C F
3 Bonaventure, TF
proc print data=a(keep = x); 4 Johnson, R F
run; 5 LaMance, K M
6 Jones, M M
7 Reberson, P F
8 King, E M
9 Pitts, D M

*CAT function with a series The SAS System 00:44 Wednesday, July 1, 2009
*The cat just concatenates the value of
variables, it just concatenates and does Obs x1 x2 x3 string
not remove space, leading or trailing;
1 a b c a bc
data a;
x1=' a';
x2=' b';
x3='c';
string=cat(of x1-x3);
run;
proc print data=a;
run;

*CAT function with a series of vars with The SAS System 00:44 Wednesday, July 1, 2009
colon
*The cat just concatenates the value of Obs x1 x2 x3 string
variables, it just concatenates and does
not remove space, leading or trailing; 1 a b c a bc

data a;
x1=' a';
x2=' b';
x3='c';
string=cat(of x:);
run;
proc print data=a;
run;

26. CATS
The cats just concatenates the value of variables. The cats is just equal to Strip or trim(left(var));.

The CATS function specifies a constant, variable, or expression, either character or numeric. If item is numeric, then
its value is converted to a character string by using the BESTw. format. In this case, SAS does not write a note to the
log.

Length of Returned Variable


In a DATA step, if the CATS function returns a value to a variable that has not previously been assigned a length, then
that variable is given a length of 200 bytes. If the concatenation operator (||) returns a value to a variable that has
not previously been assigned a length, then that variable is given a length that is the sum of the lengths of the values
which are being concatenated.

The cats function creates string of 200 bytes in length;

Function Equivalent Code


CATS(OF X1-X4) TRIM(LEFT(X1))||TRIM(LEFT(X2))||TRIM(LEFT(X3))||TRIM(LEFT(X4))
data a; The SAS System 00:44 Wednesday, July 1, 2009
set sasuser.admit; Obs x
x=cats(name,sex); 1 Murray, WM
run; 2 Almers, CF
3 Bonaventure, TF
proc print data=a (keep = x); 4 Johnson, RF
run; 5 LaMance, KM
6 Jones, MM
7 Reberson, PF
8 King, EM
9 Pitts, DM
*CATS function with a series The SAS System 00:44 Wednesday, July 1, 2009
*The cats just concatenates the value of
variables, it concatenates and removes Obs x1 x2 x3 string
space, leading or trailing;
1 a b c abc
data a;
x1=' a ';
x2=' b';
x3=' c ';
string=cats(of x1-x3);
run;
proc print data=a;
run;
*CATS function with a series of vars with The SAS System 00:44 Wednesday, July 1, 2009
colon
Obs x1 x2 x3 string
data a;
x1=' a'; 1 a b c abc
x2=' b ';
x3=' c ';
string=cats(of x:);
run;
proc print data=a;
run;

27. CATT
CatT is equal to TRIM. The catt just concatenates the value of variables, after applying the TRIM on them

The CATT function specifies a constant, variable, or expression, either character or numeric. If item is numeric, then
its value is converted to a character string by using the BESTw. format. In this case, leading blanks are removed and
SAS does not write a note to the log.

Length of Returned Variable


In a DATA step, if the CATT function returns a value to a variable that has not previously been assigned a length, then
that variable is given a length of 200 bytes. If the concatenation operator (||) returns a value to a variable that has
not previously been assigned a length, then that variable is given a length that is the sum of the lengths of the values
which are being concatenated.

The cats function creates string of 200 bytes in length;

Function Equivalent Code


CATT(OF X1-X4) TRIM(X1)||TRIM(X2)||TRIM(X3)||TRIM(X4)
The SAS System 00:44 Wednesday, July 1, 2009
data a; Obs x
set sasuser.admit; 1 Murray, WM
x=catt(name,sex); 2 Almers, CF
run; 3 Bonaventure, TF
4 Johnson, RF
proc print data=a(keep = x); 5 LaMance, KM
run; 6 Jones, MM
7 Reberson, PF
8 King, EM
9 Pitts, DM
*CATT function with a series The SAS System 00:44 Wednesday, July 1, 2009
data a;
x1=' a'; Obs x1 x2 x3 string
x2=' b';
x3='c'; 1 a b c a bc
string=catt(of x1-x3);
run;

proc print data=a;


run;
*CATT function with a series of vars with The SAS System 00:44 Wednesday, July 1, 2009
colon
Obs x1 x2 x3 string
data a;
x1=' a'; 1 a b c a bc
x2=' b';
x3='c';
string=catt(of x:);
run;

proc print data=a;


run;

28. FIND
* The find and index have the following differences;

1. The FIND function searches for substrings of characters in a character string, whereas the FINDC function
searches for individual characters in a character string.

2. The FIND function and the INDEX function both search for substrings of characters in a character string.
However, the INDEX function does not have the modifiers nor the start pos arguments.

235 data a;
*lets test the find function and the value 236 x='my name is amit';
returned, here score has a value 4 name is 237 score=find(x,'name');
the 4 char in string; 238 put score=;
239 run;

data a; score=4
x='my name is amit'; NOTE: The data set WORK.A has 1 observations and 2
score=find(x,'name'); variables.
put score=; NOTE: DATA statement used (Total process time):
run; real time 0.00 seconds
cpu time 0.01 seconds
*FIND function with a modifier 242 data a;
***The i modifier ignores the case of 243 x='my NAME is amit';
substring; 244 score=find(x,'name','i');
245 put score=;
data a; 246 run;
x='my NAME is amit';
score=find(x,'name','i'); score=4
put score=; NOTE: The data set WORK.A has 1 observations and 2
run; variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
***The t modifier strips the leading and cpu time 0.00 seconds
the trailing spaces in string and
substring;
251 data a;
data a; 252 x='my name is amit';
x='my name is amit'; 253 score=find(x,'name','t');
score=find(x,'name','t'); 254 put score=;
put score=; 255 run;
run;
score=4
NOTE: The data set WORK.A has 1 observation and 2
variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 second
* The Find function helps in the searching The SAS System 00:44 Wednesday, July 1, 2009
of a string in the character variable, it
returns the position of the string if Obs Name Sex Age Date Height
found else 0;
1 Reberson, P F 32 9 67
data amit; 2 Eberhardt, S F 49 27 64
set sasuser.admit; 3 Oberon, M F 28 17 62
if find(name,'be','t') gt 0; /* thet 4 Derber, B M 25 23 75
modifier here trims the trailing blanks of
the name var*/
run;

proc print data=amit;


run;
The SAS System 00:44 Wednesday, July 1, 2009
*FIND RETURNS THE STRING
POSITION************: Obs Name x
* we can find the string position 'be', 1 Murray, W 0
lets assign value of be to a var x; 2 Almers, C 0
3 Bonaventure, T 0
data amit; 4 Johnson, R 0
set sasuser.admit; 5 LaMance, K 0
x= find(name,'be','t') ; /* thet modifier 6 Jones, M 0
here trims the trailing blanks of the name 7 Reberson, P 3
var*/ 8 King, E 0
run; 9 Pitts, D 0
10 Eberhardt, S 2
proc print data=amit(KEEP = NAME X); 11 Nunnelly, A 0
run; 12 Oberon, M 2

29. COUNT
The count function is used to count the ocuurence of a substring in a string

data a; 357 data a;


x='My nam is amit kumar, my expertise is 358 x='My nam is amit kumar, my expertise is sas';
sas'; 360 count_is=count(x,'is');
362 put count_is=;
count_is=count(x,'is'); 363 run;
put count_is=; count_is=3
run; NOTE: The data set WORK.A has 1 observations and 2
variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

*Modifiers in count
***The count again take two modifiers
similar to find, the i and t, i for
ignoring the case and t for trimming the
balnk space;

30. COUNTW
This function counts the number of words in a string

data a; 364 data a;


x='my name is amit'; 365 x='my name is amit';
y=countw(x); 366 y=countw(x);
put y=; 367 put y=;
run; 368 run;

y=4
NOTE: The data set WORK.A has 1 observations and 2
variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

31. INT
The INT function returns the integer value of a numeric variable, thus discarding the decimal portion

data amit; The SAS System 00:44 Wednesday, July 1, 2009


x=123.89;
y=int(x); Obs x y
run;
1 123.89 123
proc print data=amit;
run;
32. ROUND
The ROUND function returns rounded value of a decimal number and by default it is 1, any value ge .50 would be in
the next integer

data amit; The SAS System 00:44 Wednesday, July 1, 2009


x1=123.89;
x2=123.28; Obs x1 x2 x3 x4 x5
x3=123.46; y1 y2 y3 y4 y5
x4=123.00;
x5=123.99; 1 123.89 123.28 123.46 123 123.99
124 123 123 123 124

y1=round(x1,2);
y2=round(x2);
y3=round(x3);
y4=round(x4);
y5=round(x5);
run;

proc print data=amit;


run;

33. N
The N function counts the number of non-missing values in a row

data a; The SAS System 00:44 Wednesday, July 1, 2009


x1=2;
x2=3; nvars_
x3=4; Obs x1 x2 x3 nonmiss

nvars_nonmiss=n(of x1-x3); 1 2 3 4 3
put nvars_nonmiss=;
run;

proc print data = a;


run;
**or using the colon as the wild The SAS System 00:44 Wednesday, July 1, 2009
character;
nvars_
data a; Obs x1 x2 x3 nonmiss
x1=2;
x2=3; 1 2 3 4 3
x3=4;

nvars_nonmiss=n(of x:);
put nvars_nonmiss=;
run;

proc print data = a;


run;
34. NMISS
The NMISS function counts the number of missing values in a row.

data a; The SAS System 00:44 Wednesday, July 1, 2009


x1=2;
x2=3; nvars_
x3=.; Obs x1 x2 x3 miss
nvars_miss=nmiss(of x1-x3);
put nvars_miss=; 1 2 3 . 1
run;

proc print data = a;


run;
**or using the colon as the wild The SAS System 00:44 Wednesday, July 1, 2009
character;
nvars_
data a; Obs x1 x2 x3 miss
x1=2;
x2=3; 1 2 3 . 1
x3=.;
nvars_miss=nmiss(of x:);
put nvars_miss=;
run;

proc print data = a;


run;

35. INTCK
The INTCK function returns the value of the complete interval passed between two dates, it can take diff arguments
like week, month, year.

data amit; The SAS System 00:44 Wednesday, July 1, 2009


x='31dec2010';
y='01jan2011'; Obs x y x1 y1 z1 z2 z3
x1=input(x,date9.);
y1=input(y,date9.); 1 31dec2010 01jan2011 18627 18628 0 1 1
z1=intck('week',x1,y1);
z2=intck('month',x1,y1);
z3=intck('year',x1,y1);
run;

proc print data=amit;


run;
***or make the date as a value using the The SAS System 00:44 Wednesday, July 1, 2009
d****;
Obs x y z1 z2 z3
data amit;
x='31dec2010'd; 1 18627 18628 0 1 1
y='01jan2011'd;
z1=intck('week',x,y);
z2=intck('month',x,y);
z3=intck('year',x,y);
run;

proc print data=amit;


run;
**AGE FINDING*****; The SAS System 00:44 Wednesday, July 1, 2009

data amit; Obs x y x1 y1 z3


x='13oct1981';
y='01jul2010'; 1 13oct1981 01jul2010 7956 18444 29
x1=input(x,date9.);
y1=input(y,date9.);

z3=intck('year',x1,y1);
run;

proc print data=amit;


run;
*Lets try to find the age by using the 475 data a;
system date; 476 dob='13oct1981'd;
477 age_years=intck('year',dob,today());
478 put age_years=;
data a; 479 run;
dob='13oct1981'd;
age_years=intck('year',dob,today()); age_years=36
put age_years=; NOTE: The data set WORK.A has 1 observations and 2
run; variables.
NOTE: DATA statement used (Total process time):
real time 0.05 seconds
cpu time 0.00 seconds

36. INTNX
The INTNX function increments the day, year or month on the specified date, here it increments the date by 6
weeks, means date after 6 weeks including current week.

481 data _null_;


data _null_; 482 x=intnx('week', '23mar2012'd, 2);
x=intnx('week', '23mar2012'd, 2); 483 put x date9.;
put x date9.; 484 run;
run;
01APR2012
NOTE: DATA statement used (Total process time):
real time 0.05 seconds
cpu time 0.00 seconds
* The intnx function increments the day, The SAS System 00:44 Wednesday, July 1, 2009
year or month on the specified date, you
can use put as well; Obs x z

data amit; 1 7956 31JAN1982


x='13oct1981'd;
z=put(intnx('month',x,3,'e'),date9.);
run;

proc print data=amit;


run;
* The intnx function increments the day, 496 data amit;
year or month on the specified date, you 497 x='13oct1981'd;
can use the same argument for incrementing 498 z=put(intnx('month',x,1,'same'),date9.);
1 month from the date; 499 put z=;
500 run;
data amit;
x='13oct1981'd; z=13NOV1981
z=put(intnx('month',x,1,'same'),date9.); NOTE: The data set WORK.AMIT has 1 observations and 2
put z=; variables.
run; NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
* The intnx function increments the day, 504 data amit;
year or month on the specified date, you 505 x='13oct1981'd;
can use the b or beginning argument then 506 z=put(intnx('month',x,1,'b'),date9.);
the output date would be 1 month and first 507 put z=;
day.; 508 run;

z=01NOV1981
data amit; NOTE: The data set WORK.AMIT has 1 observations and 2
x='13oct1981'd; variables.
z=put(intnx('month',x,1,'b'),date9.); NOTE: DATA statement used (Total process time):
put z=; real time 0.00 seconds
run; cpu time 0.00 seconds
* The intnx function increments the day, 511 data amit;
year or month on the specified date, you 512 x='13oct1981'd;
can use the e or end argument then the 513 z=put(intnx('month',x,1,'e'),date9.);
output date would be 1 month and first 514 put z=;
day.; 515 run;

data amit; z=30NOV1981


x='13oct1981'd; NOTE: The data set WORK.AMIT has 1 observations and 2
z=put(intnx('month',x,1,'e'),date9.); variables.
put z=; NOTE: DATA statement used (Total process time):
run; real time 0.00 seconds
cpu time 0.01 seconds

37. Datdif n yrdif


The datdif and yrdif function helps in calculating the days and year diff between the two dates.

'ACT/ACT'
uses the actual number of days between dates in calculating the number of years. SAS calculates this value as the
number of days that fall in 365-day years divided by 365 plus the number of days that fall in 366-day years divided by
366.

data amit; The SAS System 00:44 Wednesday, July 1, 2009


x='13oct1981';
y='03apr2012'; Obs x y x1 y1
x1=input(x,date9.); z3 z4
y1=input(y,date9.);
z3=yrdif(x1,y1,'actual'); 1 13oct1981 03apr2012 7956 19086
z4=datdif(x1,y1,'actual'); 30.4733 11130
run;

proc print data=amit;


run;
data amit; The SAS System 00:44 Wednesday, July 1, 2009
x='13oct1981'd;
y='03apr2012'd; Obs x y z3 z4
z3=yrdif(x,y,'actual');
z4=datdif(x,y,'actual'); 1 7956 19086 30.4733 11130
run;

proc print data=amit;


run;
*The today() function can be used as well The SAS System 00:44 Wednesday, July 1, 2009
for finding the age;
Obs dob age
data amit;
dob='13oct1981'd; 1 7956 35.7123
age=yrdif(dob,today(),'actual');
run;
proc print data=amit;
run;
The SAS System 00:44 Wednesday, July 1, 2009
data amit;
dob='13oct1981'd; Obs dob age
age=datdif(dob,today(),'actual');
run; 1 7956 13044

proc print data=amit;


run;

38. REVERSE
The reverse function just reverse the string, if there are leading blanks they become trailing.

553 data a;
data a; 554 x='abc';
x='abc'; 555 change=reverse(x);
change=reverse(x); 556 put change;
put change; 557 run;
run;
cba
NOTE: The data set WORK.A has 1 observations
and 2 variables.
NOTE: DATA statement used (Total process
time):
real time 0.35 seconds
cpu time 0.01 seconds

* Let's say you have drug data, how would you


take out the dose strength in a new variable; The SAS System 00:44 Wednesday, July
1, 2009
data a;
input trt$; Obs trt dose
datalines;
a150 1 a150 150
b120 2 b120 120
130 3 130 130
cd300 4 cd300 300
;
run;

data b;
set a;
dose=reverse(substr(reverse(compress(trt)),1,3));
run;

proc print data = b;


run; 37 Functions

You might also like