BookmarkSubscribeRSS Feed
COLDORANGE
Calcite | Level 5

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.

3 REPLIES 3
stat_sas
Ammonite | Level 13

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;

Astounding
PROC Star

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.

ballardw
Super User

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

add

idnum3 = _n_; to the same data step.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 708 views
  • 0 likes
  • 4 in conversation