Help using Base SAS procedures

Do you have any idea for adding last(or maximum) value?

Reply
Occasional Contributor
Posts: 12

Do you have any idea for adding last(or maximum) value?

help.JPG

used code

-----------------------------------------------------------------------------------------------

data tp;

  set p ;

i=index(var1,'r')  ;

if i=1 then do idnum1=input(compress(var1, ' ', 'A'), 5.) ;

end;

if i=2 then do  idnum2=input(compress(var1, ' ', 'A'), 5.) ;

end;

run;

-----------------------------------------------------------------------------------------------

I want to make new variable(idnum3) from this dataset.

How could I make 'idnum3' 1 to 20 using last observation of 'idnum1' add to 'idnum2'.

If you have any idea please tell me.

thank you.

Trusted Advisor
Posts: 1,195

Re: Do you have any idea for adding last(or maximum) value?

data have;
input var1 $;
i=index(var1,'r');
if i=1 then idnum1=input(compress(var1, ' ', 'A'), 5.) ;
if i=2 then idnum2=input(compress(var1, ' ', 'A'), 5.) ;
datalines;
rt01
rt02
rt03
rt04
rt05
rt06
rt07
rt08
rt09
rt10
tr01
tr02
tr03
tr04
tr05
tr06
tr07
tr08
tr09
tr10
;

proc sql;
select max(idnum1) into :max from have;
quit;

data want;
set have;
idnum3=sum(&max,idnum2);
run;

Respected Advisor
Posts: 4,753

Re: Do you have any idea for adding last(or maximum) value?

Assuming that the portion of code that you posted is working as intended, you could add to the same DATA step:

retain last_idnum1;

if idnum1 > . then do;

  idnum3 = idnum1;

  last_idnum1 = idnum1;

end;

else idnum3 = sum(last_idnum1, idnum2);

Good luck.

Grand Advisor
Posts: 10,026

Re: Do you have any idea for adding last(or maximum) value?

And if you want a simple row counter, assuming you may have more data

add

idnum3 = _n_; to the same data step.

Ask a Question
Discussion stats
  • 3 replies
  • 147 views
  • 0 likes
  • 4 in conversation