Sas If Then Else

Topics: Output, Data, Quotation mark Pages: 12 (1347 words) Published: February 11, 2013
Using IF THEN ELSE statements, LENGTH is to be use so that the new coloum create (Patient) will have defined length that is \$12.

data newclass;
set sashelp.class;
length Patient \$12.;
If weight <=84 then Patient='UnderWeight';
else
If weight >84 and weight <=100 then Patient='ProperWeight';
else
If weight >100 then Patient= 'OverWeight';
else
Patient= 'OutLier';
run;

====================================================================== SELECT WHEN END statement can also be use in place of IF THEN ELSE statement. LENGTH is to be use so that the new coloum create (Patient) will have defined length that is \$12.

data newclass;
set sashelp.class (keep= name weight);
Select;
when (weight >84 and weight <=100) Grade= 'Medium';
end;
-------------------------------------------------
run;

data Cars_New;
set sashelp.cars (keep= Make MSRP);
Length sales \$14.;
select;
when (MSRP <=10500) sales= 'Lowest';
when (MSRP > 10500 and MSRP <= 18000) sales= 'Medium';
when (MSRP > 18000) sales = 'Heighest';
otherwise sales= 'outlier';
End;
run;
=================================================================================== To create more then one dataset from one dataset

data Cars_SUV Cars_SEDAN Cars_OTHERS;
set sashelp.cars ;
If Type = 'SUV' then output Cars_SUV;
else
If Type = 'Sedan' then output Cars_SEDAN;
else
output Cars_OTHERS;
RUN;

RETAIN Statement: It initialize a value to a variable

Do Loops; Leave

Data detail;
do var= 1 to 10;
if var=7 then leave; (Leave will tel progm to stop when reach Var=7) else;
output;
end;
run;

Do Loops; continue

Data detail;
Do var= 1 to 15;
if var= 8 then continue; (Continue will tel program to continue when Var=7) else;
output;
end;
run;

Do Until: until condition is false

Data detail;
N=1;------------------------- Minimum value of N=1
Do until (N>10);--------------- Condition
output;
N= N+3;---------------------- Incremental value of N is 3
end;
Label N= Var;

run;

Do while: Condition is true

Data detail;
N=1; -------------------------Minimum value of N=1
Do until (N>10); ---------------Condition
output;
N= N+3; ----------------------Incremental value of N is 3
end;
Label N= Var;
run;

Put Statement: to print output in the sas log Window.
File print: to print the put value in the output window

Options:
Keep
Drop
Level

data cl;
set sashelp.class (keep= Name Sex height);
file print;
Put @1 Name @12 Sex @14 height;
run;

File statement create a new file outside the sas environment in C drive, S drive etc data cl;
set sashelp.class (keep= Name Sex height);
file "S:\Workshop\sis.txt";
Put @1 Name @12 Sex @14 height;

run;

Delete: to delete particular observation

data vish.class;
set sashelp.class;
if age=14 or age=15 then delete; (here to delete the obsevtion with age 14 and 15) run;

Error statement is use to print the comment in the log window, it is same as Put statement.

Stop statement: is use to stop the programme if condition is fulfil.

data vish.cars;
set sashelp.cars;
if MSRP > 37000 then;
do;
error "This is not the correct MSRP";
stop;
end;
run;

DLM = : To read the comma value and wildcards from external files

data vish.Emp2;
infile "'S:\Workshop\Vish\EMP2.txt" dlm= ',’;
input EMPNo Name \$ Job \$ Sal;
run;

data vish.Emp2;
infile "'S:\Workshop\Vish\EMP3.txt" dlm= '@#\$%&' ;
input EMPNo Name \$ Job \$ Sal;
run;

DSD: To read Single Quote (‘) Double Quote (“) Comma (,)We ac use DSD option if Comma will be there also

data vish.Emp5;
infile "'S:\Workshop\Vish\EMP5.txt" DSD ;
input EMPNo Name \$ Job \$ Sal;
run;

Expandtabs: If in the raw file spaces are not in proper manner, we use expandtabs so that data are...