BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
joon1
Quartz | Level 8

Dear Madam/Sir,

 

The data set is as follows:

 

Obs rdate cusip ticker held_pct cyear1234567891011121314151617181920
03/31/202100032Q10AADI0.043812021
09/30/202100032Q10AADI0.091912021
12/31/202100032Q10AADI0.079642021
03/31/202200032Q10AADI0.078542022
06/30/202200032Q10AADI0.078492022
09/30/202200032Q10AADI0.078212022
12/31/202200032Q10AADI0.054402022
03/31/202300032Q10AADI0.013022023
03/31/199900036020AAON0.148981999
06/30/199900036020AAON0.167821999
09/30/199900036020AAON0.174311999
12/31/199900036020AAON0.174961999
03/31/200300036020AAON0.000022003
06/30/200300036020AAON0.000052003
09/30/200300036020AAON0.000102003
12/31/200300036020AAON0.000122003
03/31/200400036020AAON0.000122004
06/30/200400036020AAON0.000122004
09/30/200400036020AAON0.000132004
12/31/200400036020AAON0.000122004

 

I tried to select last quarter data from quarterly data using 'last.' function as follows:

 

data i2; set i1; by cusip rdate;
q4=last.rdate; run;

 

However, q4  is assigned for all observations. 

 

And the following code does not work either.

data i2; set i1; by cusip rdate;
if last.rdate; run;

 

NOTE: There were 213968 observations read from the data set WORK.IS2.
NOTE: The data set WORK.IS3 has 213968 observations and 5 variables.

The SAS instructions online does not work well. Any help will be highly appreciated.

 

Sincerely,

Joon1

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You seem to only have one observation per DATE value.  So every observations is going to be both FIRST.DATE and LAST.DATE.

 

You would need to have YEAR variable.  Then if you have 4 quarters per YEAR you could use LAST.YEAR to select the last available QTR for the year.

data for_analysis;
  set have;
  year=year(RDATE);
run;
data want;
  set have;
  by cusip year rdate;
  if last.year;
run;

The first data step could be a VIEW if the data is large.

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

You seem to only have one observation per DATE value.  So every observations is going to be both FIRST.DATE and LAST.DATE.

 

You would need to have YEAR variable.  Then if you have 4 quarters per YEAR you could use LAST.YEAR to select the last available QTR for the year.

data for_analysis;
  set have;
  year=year(RDATE);
run;
data want;
  set have;
  by cusip year rdate;
  if last.year;
run;

The first data step could be a VIEW if the data is large.

joon1
Quartz | Level 8
Thank you so much, Tom. Appreciated.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 697 views
  • 0 likes
  • 2 in conversation