Wednesday, November 18, 2009

COMPRESS with third argument

To remove all non-printable characters from your string i.e all ASCII's below 32 and above 126, you can do by providing "KW" (keep writable) as THIRD argument to compress function.
Example
data eject_a_page;
name = "Vikas" || '12'x || " Gaddu";
newname = compress(name,,"kw");
run;

do leave loop and continue !!!

There are two statements in SAS, leave and continue. Difference between two is seldom not understood.

data leave;
do i = 1 to 5;
do j = 1 to 5;
if j = 2 or j = 4 then leave;
output;
end;
end;
run;

Here SAS will leave inner do loop completely once the first condition j=2 gets satisfied. So in output you will never have j = 2 or 3 or 4 or 5.

OUTPUT
i j
1 1
2 1
3 1
4 1
5 1

Same program with continue statement.


data continue;
do i = 1 to 5;
do j = 1 to 5;
if j = 2 or j = 4 then continue;
output;
end;
end;
run;

Here SAS will skip the inner loop for both conditions j = 2 and j=4, but it will not completely leave the loop. So ouput for this will look like

i j
1 1
1 3
1 5
2 1
2 3
2 5
. .
. .
. .
5 3
5 5

Thursday, June 25, 2009

SUBSTR function can be used on both side of assignment statment

data name;
name = "vikus";
substr(name,4,1) = "a";
run;

proc print ;
run;

This will change u to a in name variable.

data name;
name = "Vikas Gaddu";
firstname = substr(name,1,5);
run;

proc print;
run;

Firstname variable will contain Vikas.