Start Day 2;



Reading selected observations or rows:

* Read the input data for the first 6 observations;

Data stocks;

Set cdat.stocks(OBS=6);

Run;

Note: OBS= dataset option can only be used for input dataset.

If the first row of the input text file contains the variable name, from the second row start the data, use the FIRSTOBS=2 option.

Example:

filename bb '/courses/ddbf9765ba27fe300/bill.txt';

data bill;

infile bb FIRSTOBS=2;

input fname $1-13 lname $14-25 ssn1 ssn2 ssn3 areacd phonenum $

@68 bal1 dollar8. @77 duedt yymmdd10. @88 billdt date9.;

run;

Combined use of firstobs and obs options:

Libname cdat '/courses/ddbf9765ba27fe300';

Data stocks;

Set cdat.stocks(FIRSTOBS=5 OBS=6);

Run;

Output_dataset_obs = OBS – FIRSTOBS + 1 ;

Reading selected observations with logical comparison:

Libname cdat '/courses/ddbf9765ba27fe300';

Data stocks;

Set cdat.stocks2;

If _n_ > 5;

If industry='PHAR';

Run;

Note: _n_ is a SAS system variable generated at run time. It’s value is row number.

Data stocks;

Set cdat.stocks;

If price=. Then delete;

Run;

Data stocks;

Set cdat.stocks2;

Where industry='PHAR';

Run;

Data stocks (Where =(industry='PHAR'));

Set cdat.stocks2;

Run;

Data stocks ;

Set cdat.stocks2(Where =(industry='PHAR'));

Run;

proc print data=cdat.stocks2;

where industry='PHAR';

run;

• WHERE is more efficient than if. When using WHERE, only those selected observation will be read. While using IF condition, all observations will be read, only those selected observations will be output to the output data set.

• WHERE can not be used with data step options such as: OBS=, FIRSTOBS=, POINT=, ETC.

• WHERE can not be used with execution time system variable _N_.

• WHERE statement can be used in both DATA STEP and PROC STEP. IF statement can only be used DATA STEP.

• Notice the difference between using dataset option for input data set and output data set.

When the option is used for the input data set, only those observations that fit the condition will be read into the data step. When the option is used for the output data set, all observations will be read, but only those observations that fit the condition will be output to the output data set.

Example: The following WHERE statement using variable _n_ get ERROR.

Data stocks;

Set cdat.stocks;

where _n_ string len,;

put 'pos2=' pos2; * it search left ward;

string3='The search is Case Sensitive';

pos3=find(string3, 'case'); * pos3=0, cannot find, case sensitive;

put 'pos3=' pos3;

string4='The search is not Case Sensitive';

pos4=find(string4, 'case', 'i'); * ignore case, pos4=19;

put 'pos4=' pos4;

*** equivalent using index function;

string5=upcase(string4); * pos5=19;

index5=index(string5,'CASE');

put 'index5=' index5;

run;

A string function example:

filename mod /courses/ddbf9765ba27fe300/model_out.txt';

data mod;

infile mod truncover;

input line $1-256;

if scan(line,1, ' ') in ('if', 'else');

run;

data mod2;

length varname $32 yname $8 yval 8;

retain vn;

set mod;

if scan(line,1) = 'else' then do;

varname=substr(line,21, 32);

yname =substr(line,71, 8);

yval =substr(line,83, 8);

end;

else do;

varname=scan(line,2, ' ');

yname =scan(line,-3, ' ;');

yval =scan(line,-1, ' ;');

end;

varname = scan(varname,1);

if varname ne '' then vn=varname;

else if varname = '' then varname=vn;

run;

proc sort data=mod2 out=mod3;

by varname descending yval;

run;

/*

1 2 3 4 5 6 7 8 9

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890

if ALL903 = 0 then YIZQ0015 = -0.2021 ;

else if 0 < ALL903 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download