BookmarkSubscribeRSS Feed
harrylui
Obsidian | Level 7

good day,

below is my program structure;

 

              test1 test2                           Expected net

row1      100  90                                     10

row2      99    90                                      9

row3      98    90                                      8

row4      97    90                                      7

net=test1-test2;

if row(n) net=10 then row(n+1) test1 = 100 ;

if row(n) net=10 then row(n+1) test2 = 90 ;

 

the problem is when the SAS condition change row2 test1=100 and test2=90

you have to rerun the data step and get the correct net for row2

and after that row3 can meet the condition and make its changing

 

 

expected result:

            test1 test2  net

row1    100  90      10

row2    100  90      10

row3    100  90      10

row4    100  90      10

the only solution i can figure out for now is manually run this script for 3 times then i can get my expected result.

so i am going to seek help in this forum.

anyone get a better way to do it?

 

Thanks in advance,

Harry

 

 

1 REPLY 1
ed_sas_member
Meteorite | Level 14

Hi @harrylui 

 

Does the following code could meet your expectations?

data have;
	input test1 test2;
	datalines;
100 90
99 90
98 90
97 90
;
run;

data want;
	set have;
	if _n_=1 then net=test1-test2;
	retain net;
	test1 = 100;
	test2 = 90;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1 reply
  • 301 views
  • 0 likes
  • 2 in conversation