BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BellaLuna
Fluorite | Level 6

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
1 ACCEPTED SOLUTION

Accepted Solutions
kannand
Lapis Lazuli | Level 10

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

2 REPLIES 2
kannand
Lapis Lazuli | Level 10

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
BellaLuna
Fluorite | Level 6
Thank you so much! This helps a lot!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 985 views
  • 1 like
  • 2 in conversation