Desktop productivity for business analysts and programmers

coding help please!!!

Accepted Solution Solved
Reply
Regular Contributor
Posts: 220
Accepted Solution

coding help please!!!

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*/


Accepted Solutions
Solution
‎11-26-2013 06:09 PM
Super User
Super User
Posts: 6,323

Re: coding help please!!!

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;

View solution in original post


All Replies
Solution
‎11-26-2013 06:09 PM
Super User
Super User
Posts: 6,323

Re: coding help please!!!

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;

Regular Contributor
Posts: 220

Re: coding help please!!!

Thanks a lot Tom - it ran fine...

can you please give little more explanation on

return;

last:

output;

Esteemed Advisor
Posts: 7,293

Re: coding help please!!!

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.

Regular Contributor
Posts: 220

Re: coding help please!!!

thanks all!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 275 views
  • 3 likes
  • 3 in conversation