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.

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
  • 2 replies
  • 181 views
  • 0 likes
  • 2 in conversation