BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas_9
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

4 REPLIES 4
Tom
Super User Tom
Super User

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;

sas_9
Obsidian | Level 7

Thanks a lot Tom - it ran fine...

can you please give little more explanation on

return;

last:

output;

art297
Opal | Level 21

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.

sas_9
Obsidian | Level 7

thanks all!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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