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!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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