DATA Step, Macro, Functions and more

pseudo codes execution: if VAR(t+1)>VAR(t) then output VAR(t)

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

pseudo codes execution: if VAR(t+1)>VAR(t) then output VAR(t)

[ Edited ]

Hello!

I'm just starting to learn SAS, I have a seemingly straightforward logic that I don't know how to execute. If possible, would you please provide me an Example coding?

What I need to do is:

 

If within the same code, Interest(t+1)is less than Interest(t), then output coupon=interest(t), keep date, code. In the example below, it is observation 6.

 

obs.    date    code    interest
1   1/1/2015    1   0.2
2   1/2/2015    1   0.5
3   1/3/2015    1   1.9
4   1/4/2015    1   2.5
5   1/5/2015    1   3.8
6   1/1/2015    2   2.1
7   1/2/2015    2   0
8   1/3/2015    2   0.1
9   1/4/2015    2   0.2
10  1/5/2015    2   0.3

 

output sample should look like either:

obs.    date    code    interest  coupon
1       1/1/2015     2    2.1      2.1

or

obs.    date    code    interest  coupon
1   1/1/2015    1        0.2
2   1/2/2015    1        0.5
3   1/3/2015    1        1.9
4   1/4/2015    1        2.5
5   1/5/2015    1        3.8
6   1/1/2015    2        2.1       2.1
7   1/2/2015    2        0
8   1/3/2015    2        0.1
9   1/4/2015    2        0.2
10  1/5/2015    2        0.3

Accepted Solutions
Solution
‎11-23-2015 09:55 PM
Regular Contributor
Posts: 161

Re: pseudo codes execution: if VAR(t+1)>VAR(t) then output VAR(t)

You may be able to accomplish this result using multiple ways such as SQL joins, Data steps,etc. But here is one way using "DATA STEP"

 

DATA HAVE;
INPUT OBS:3. DATEIN:$9. CODE:$1. INTEREST:3.1;
DATALINES;
1   1/1/2015    1   0.2
2   1/2/2015    1   0.5
3   1/3/2015    1   1.9
4   1/4/2015    1   2.5
5   1/5/2015    1   3.8
6   1/1/2015    2   2.1
7   1/2/2015    2   0
8   1/3/2015    2   0.1
9   1/4/2015    2   0.2
10  1/5/2015    2   0.3
;
RUN;
DATA WANT(KEEP=OBS DATEF CODE PREV_INTEREST COUPON); 
	SET HAVE; 
	DATEF=INPUT(DATEIN,ANYDTDTE.);
	RETAIN PREV_OBS PREV_DATEF PREV_CODE PREV_INTEREST;		
	IF (INTEREST LT PREV_INTEREST) AND (CODE = PREV_CODE)
	THEN 
		DO;
			COUPON=PREV_INTEREST;
			OUTPUT ;
		END;
	PREV_OBS=OBS;
	PREV_DATEF=DATEF;
	PREV_CODE=CODE;
	PREV_INTEREST=INTEREST;
RUN;
PROC PRINT;
	FORMAT DATEF MMDDYY10.;
RUN;

Yields this output....

Obs	OBS	CODE	DATEF	PREV_INTEREST	COUPON
1	7	2	01/02/2015	2.1	2.1

Hope this helps. Good Luck...!!!

Kannan Deivasigamani

View solution in original post


All Replies
Solution
‎11-23-2015 09:55 PM
Regular Contributor
Posts: 161

Re: pseudo codes execution: if VAR(t+1)>VAR(t) then output VAR(t)

You may be able to accomplish this result using multiple ways such as SQL joins, Data steps,etc. But here is one way using "DATA STEP"

 

DATA HAVE;
INPUT OBS:3. DATEIN:$9. CODE:$1. INTEREST:3.1;
DATALINES;
1   1/1/2015    1   0.2
2   1/2/2015    1   0.5
3   1/3/2015    1   1.9
4   1/4/2015    1   2.5
5   1/5/2015    1   3.8
6   1/1/2015    2   2.1
7   1/2/2015    2   0
8   1/3/2015    2   0.1
9   1/4/2015    2   0.2
10  1/5/2015    2   0.3
;
RUN;
DATA WANT(KEEP=OBS DATEF CODE PREV_INTEREST COUPON); 
	SET HAVE; 
	DATEF=INPUT(DATEIN,ANYDTDTE.);
	RETAIN PREV_OBS PREV_DATEF PREV_CODE PREV_INTEREST;		
	IF (INTEREST LT PREV_INTEREST) AND (CODE = PREV_CODE)
	THEN 
		DO;
			COUPON=PREV_INTEREST;
			OUTPUT ;
		END;
	PREV_OBS=OBS;
	PREV_DATEF=DATEF;
	PREV_CODE=CODE;
	PREV_INTEREST=INTEREST;
RUN;
PROC PRINT;
	FORMAT DATEF MMDDYY10.;
RUN;

Yields this output....

Obs	OBS	CODE	DATEF	PREV_INTEREST	COUPON
1	7	2	01/02/2015	2.1	2.1

Hope this helps. Good Luck...!!!

Kannan Deivasigamani
Occasional Contributor
Posts: 10

Re: pseudo codes execution: if VAR(t+1)>VAR(t) then output VAR(t)

Thank you so much! This helps a lot!
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 191 views
  • 1 like
  • 2 in conversation