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

Dear altruists,
I want to create a new dataset based on a specific calendar date as follows:


My reference date is 13 April 2009.
I want to retain observations containing only Fiscal_Quarter 2 which immediately precedes the first Fiscal_Quarter 4 before 13 April 2009.
So for instance, for Company_ID 1210, the first Fiscal_Quarter 4 before 13 April 2009 is on 31 March 2009.
I want to retain the Fiscal_Quarter 2 value on 30 September 2008.

I have the following dataset (I have uploaded the SAS dataset in the end):

Company_IDDateFiscal_QuarterReturn
12102008063010.243333
12102008093020.443333
12102008123130.483333
12102009033140.363333
12102009063010.163333
12102009093020.203333
10942007093010.243333
10942007123120.283333
10942008033130.323333
10942008063040.363333
10942008093010.403333
10942008123120.443333
10942009033130.483333
10942009063040.363333
13272007123110.163333
13272008033120.123333
13272008063030.027333
1327200809304-0.06867
1327200812311-0.16467
1327200903312-0.26067
1327200906303-0.35667
1327200909304-0.45267
1045200803311-0.54867
1045200806302-0.64467
1045200809303-0.74067
1045200812314-0.83667
1045200903311-0.93267
1045200906302-1.02867

 

I am expecting the following output:

Company_IDDateFiscal_QuarterReturn
12102008093020.443333
10942007123120.283333
13272008033120.123333
1045200806302-0.64467


You would notice that for Company_ID 1327, I do not want the row for Fiscal_Quarter 2 on 31 March 2009 since the first value for Fiscal_Quarter 4 is on 30 September 2008.

Thank you in advance for all your support!

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Assuming that "Date" is a sas-date:

proc sort data=work.have out= sorted;
   where Date < '13Apr2009'd and Fiscal_Quarter in (2, 4);
   by Company_ID descending Date;
run;

data work.result;
   set work.sorted;
   by Company_ID descending Date;
   retain output_next 0;

   if fiscal_quarter = 4 then output_next = 1;

   if fiscal_quarter = 2 and output_next = 1 then do;
      output;
      output_next = 0;
   end;

   drop output_next;
run;

proc sort data=work.result out=work.want;
   by descending return;
run;

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

I want to retain observations containing only Fiscal_Quarter 2 which immediately precedes the first Fiscal_Quarter 4 before 13 April 2009.

 

Here is UNTESTED CODE. If you want tested code, please provide the data as SAS data step code (instructions).

 

/* Find fiscal quarter 4 before 13APR2009 */
proc sql;
    create last_fiscal_quarter4 as select * from have(where=(fiscal_quarter=4 and date<'13APR2009'd))
    group by company_id
    having date=max(date);
run;
/* Find fiscal quarter 2 before the above found date */
proc sql;
    create want as select h.*
    from have(where=(fiscal_quarter=2)) as h left join last_fiscal_quarter4 as l
    on h.company_id=l.company_id
    where h.fiscal_quarter<l.date
    group by company_id
    having h.date=max(h.date);
quit;
--
Paige Miller
mmh
Obsidian | Level 7 mmh
Obsidian | Level 7

Thank you for your kind response.
Unfortunately, I could not convert it into datastep since I have never done it before 😞
I am working on it.
Just to let you know that I received the following error message when I ran your untested code:

b.PNG

I will upload the file again as a datastep again in my query if hopefully I can figure it out.
Thanks again!

PaigeMiller
Diamond | Level 26

Fixed (I think). Silly error on my part.

 

/* Find fiscal quarter 4 before 13APR2009 */
proc sql;
    create table last_fiscal_quarter4 as select * from have(where=(fiscal_quarter=4 and date<'13APR2009'd))
    group by company_id
    having date=max(date);
run;
/* Find fiscal quarter 2 before the above found date */
proc sql;
    create table want as select h.*
    from have(where=(fiscal_quarter=2)) as h left join last_fiscal_quarter4 as l
    on h.company_id=l.company_id
    where h.fiscal_quarter<l.date
    group by company_id
    having h.date=max(h.date);
quit;
--
Paige Miller
mmh
Obsidian | Level 7 mmh
Obsidian | Level 7

Thanks a lot again Andreas!
Unfortunately, there was still a small error in the log which I could not fix.

l.PNG

Nonetheless, I have got the solution, thanks to Andreas 🙂
I really appreciate your time!

andreas_lds
Jade | Level 19

Assuming that "Date" is a sas-date:

proc sort data=work.have out= sorted;
   where Date < '13Apr2009'd and Fiscal_Quarter in (2, 4);
   by Company_ID descending Date;
run;

data work.result;
   set work.sorted;
   by Company_ID descending Date;
   retain output_next 0;

   if fiscal_quarter = 4 then output_next = 1;

   if fiscal_quarter = 2 and output_next = 1 then do;
      output;
      output_next = 0;
   end;

   drop output_next;
run;

proc sort data=work.result out=work.want;
   by descending return;
run;
mmh
Obsidian | Level 7 mmh
Obsidian | Level 7
Thank you so much Andreas!
I am really grateful for your kind support!
mmh
Obsidian | Level 7 mmh
Obsidian | Level 7

Hello Andreas,
Thank you for your reply the other day.
When I am trying to run the program on a larger dataset, unfortunately the results are not coming as expected.
For instance, the result for GVKEY 001209, GVKEY 004321.
Would be grateful if you could kindly give me any suggestions as to how I can fix this issue.
I am attaching the larger dataset here.
Since I have modified the names of the columns a bit, I am copying your same code with the names a bit modified here for your convenience.

proc sort data=work.have out= sorted1;
where Datadate < '13Apr2009'd and FQTR in (2, 4);
by GVKEY descending Datadate;
run;

data work.result1;
set work.sorted1;
by GVKEY descending Datadate;
retain output_next 0;

if FQTR = 4 then output_next = 1;

if FQTR = 2 and output_next = 1 then do;
output;
output_next = 0;
end;

drop output_next;
run;

proc sort data=work.result1 out=work.want1;
by descending GVKEY Datadate;
run;

andreas_lds
Jade | Level 19

Sorry, but i can't download any binary files. Could you post the data of GVKEY=001209 and GVKEY=004321 as data step?

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 3906 views
  • 2 likes
  • 3 in conversation