data perm.residents;
infile census;
retain Address;
data perm.residents;
infile census;
retain Address;
input type $1. @;
data perm.residents;
infile census;
retain Address;
input type $1. @;
if type='H' then do;
data perm.residents; infile census; retain Address; input type $1. @; if type='H' then do; if _n_ > 1
data perm.residents; infile census; retain Address; input type $1. @; if type='H' then do; if _n_ > 1 then output;
0
. data perm.residents;
infile census;
retain Address;
input type $1. @;
if type='H' then do;
if _n_ > 1 then output;
Total=0;
data perm.residents;
infile census;
retain Address;
input type $1. @;
if type='H' then do;
if _n_ > 1 then output;
Total=0;
input address $ 3-17;
data perm.residents;
infile census;
retain Address;
input type $1. @;
if type='H' then do;
if _n_ > 1 then output;
Total=0;
input address $ 3-17;
end;
H
, you need
to define an alternative action. You can do this by adding an ELSE
statement after the DO group.
data perm.residents; infile census; retain Address; input type $1. @; if type='H' then do; if _n_ > 1 then output; Total=0; input address $ 3-17; end; else
data perm.residents;
infile census;
retain Address;
input type $1. @;
if type='H' then do;
if _n_ > 1 then output;
Total=0;
input address $ 3-17;
end;
else if type='P' then
0
. Now, as each detail record
is read, you can increment the value of Total by using a sum statement.
In this example, you are counting the number of detail records for
each header record. Therefore, you increment the value of Total by
1 when the value of type is P
. data perm.residents; infile census; retain Address; input type $1. @; if type='H' then do; if _n_ > 1 then output; Total=0; input address $ 3-17; end; else if type='P' then total+1;
else if type='B' then total+cost;
The
value generated by a sum statement is automatically retained throughout
the DATA step. That is why it is important to set the value of Total
to 0
each time a header record is read.
data perm.residents; infile census; retain Address; input type $1. @; if type='H' then do; if _n_ > 1 then output; Total=0; input address $ 3-17; end; else if type='P' then total+1;
Syntax, INFILE statement
with the END= option:
INFILE file-specification END=variable;
0 until
the last line is read and 1 after the
last line is read.
Note: Like automatic variables,
the END= variable is not written to the data set.
|
0
, the
OUTPUT statement writes the final observation to the data set. data perm.residents; infile census end=last; retain Address; input type $1. @; if type='H' then do; if _n_ > 1 then output; Total=0; input address $ 3-17; end; else if type='P' then total+1; if last then output;
data perm.residents (drop=type); infile census end=last; retain Address; input type $1. @; if type='H' then do; if _n_ > 1 then output; Total=0; input address $ 3-17; end; else if type='P' then total+1; if last then output; run;