hi guys, i am trying to get total amount for two patient but in below code i am getting only one obs. instead of two. can someone plz help...
Thanks!
data test (drop=type amount);
infiles cards end=last;
retain id name;
input type $1. @;
if type='p' then do;
if _n_ >1 then output;
totalcost=0;
input @3 id @8 name $15.;
end;
if type='c' then do;
input @12 amount comma6.;
totalcost+amount;
end;
if last then output;
cards,
p 1095 smith, howard
c 01-08-98 $45.0
c 01-17-98 $37.0
p 1096 barclay, nick
c 01-09-98 $156.5
c 01-17-98 $20.0
;
run;
/*i think it's OUTPUT statement issue, i tried to put 'output' statement different places but didn't get correct result*/
You cannot use the END= option when using in line data. Try re-coding to use the EOF= option instead.
data test (drop=type amount);
infile cards eof=last;
retain id name;
input type $1. @;
if type='p' then do;
if _n_ >1 then output;
totalcost=0;
input @3 id @8 name $15.;
end;
if type='c' then do;
input @12 amount comma6.;
totalcost+amount;
end;
return;
last:
output;
cards;
p 1095 smith, howard
c 01-08-98 $45.0
c 01-17-98 $37.0
p 1096 barclay, nick
c 01-09-98 $156.5
c 01-17-98 $20.0
run;
You cannot use the END= option when using in line data. Try re-coding to use the EOF= option instead.
data test (drop=type amount);
infile cards eof=last;
retain id name;
input type $1. @;
if type='p' then do;
if _n_ >1 then output;
totalcost=0;
input @3 id @8 name $15.;
end;
if type='c' then do;
input @12 amount comma6.;
totalcost+amount;
end;
return;
last:
output;
cards;
p 1095 smith, howard
c 01-08-98 $45.0
c 01-17-98 $37.0
p 1096 barclay, nick
c 01-09-98 $156.5
c 01-17-98 $20.0
run;
Thanks a lot Tom - it ran fine...
can you please give little more explanation on
return;
last:
output;
Like Tom mentioned, you can't use eof in an infile statement when you are using cards or datalines. Instead, you can use eof=label. What Tom's code did was create a label called 'last'.
As such, when the code hit the return statement, it skipped the label (and its code) and returned to the start of the code. When it got to the last record, it jumped down to the last label, output the record, then exited.
thanks all!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.