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 am a new user of SAS.

I have the following table:

GVKeyMonthAnnouncementsAnn_Yes/No
1001Jan 0
1001Feb 0
1001MarMar1
1001Apr 0
1001May 0
1001JunJun1
1001Jul 0
1001Aug 0
1001SepSep1
1001Oct 0
1001Nov 0
1001DecDec1
1002Jan 0
1002FebFeb1
1002Mar 0
1002Apr 0
1002May 0
1002JunJun1
1002Jul 0
1002Aug 0
1002SepSep1
1002Oct 0
1002NovNov1
1002Dec 0


I am trying to create an output column "Ann_in_last_12_months" where I count the previous 12 rows by group on the basis of the "Ann_Yes/No" column and add up the resulting value as shown below:

 

GVKeyMonthAnnouncementsAnn_Yes/NoAnn_in_last_12_months
1001Jan 00
1001Feb 00
1001MarMar11
1001Apr 01
1001May 01
1001JunJun12
1001Jul 02
1001Aug 02
1001SepSep13
1001Oct 03
1001Nov 03
1001DecDec14
1002Jan 00
1002FebFeb11
1002Mar 01
1002Apr 01
1002May 01
1002JunJun12
1002Jul 02
1002Aug 02
1002SepSep13
1002Oct 03
1002NovNov14
1002Dec 04

 

Thank you in advance for your kind support!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Here is one way 

 

data have;
input GVKey $ Month $ Announcements $ y_n;
infile datalines dlm = ',';
datalines;
1001,Jan,   ,0
1001,Feb,   ,0
1001,Mar,Mar,1
1001,Apr,   ,0
1001,May,   ,0
1001,Jun,Jun,1
1001,Jul,   ,0
1001,Aug,   ,0
1001,Sep,Sep,1
1001,Oct,   ,0
1001,Nov,   ,0
1001,Dec,Dec,1
1002,Jan,   ,0
1002,Feb,Feb,1
1002,Mar,   ,0
1002,Apr,   ,0
1002,May,   ,0
1002,Jun,Jun,1
1002,Jul,   ,0
1002,Aug,   ,0
1002,Sep,Sep,1
1002,Oct,   ,0
1002,Nov,Nov,1
1002,Dec,   ,0
;

data want;
    array l[0:11] _temporary_;            
    call missing(of l[*], obs);   
    
    do obs = 1 by 1 until (last.GVKey);       
        set have;
        by GVKey;
        l[mod(obs, 12)] = y_n;            
        count = sum(of l[*]);              
        output;
    end;
    
run;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Here is one way 

 

data have;
input GVKey $ Month $ Announcements $ y_n;
infile datalines dlm = ',';
datalines;
1001,Jan,   ,0
1001,Feb,   ,0
1001,Mar,Mar,1
1001,Apr,   ,0
1001,May,   ,0
1001,Jun,Jun,1
1001,Jul,   ,0
1001,Aug,   ,0
1001,Sep,Sep,1
1001,Oct,   ,0
1001,Nov,   ,0
1001,Dec,Dec,1
1002,Jan,   ,0
1002,Feb,Feb,1
1002,Mar,   ,0
1002,Apr,   ,0
1002,May,   ,0
1002,Jun,Jun,1
1002,Jul,   ,0
1002,Aug,   ,0
1002,Sep,Sep,1
1002,Oct,   ,0
1002,Nov,Nov,1
1002,Dec,   ,0
;

data want;
    array l[0:11] _temporary_;            
    call missing(of l[*], obs);   
    
    do obs = 1 by 1 until (last.GVKey);       
        set have;
        by GVKey;
        l[mod(obs, 12)] = y_n;            
        count = sum(of l[*]);              
        output;
    end;
    
run;
mmh
Obsidian | Level 7 mmh
Obsidian | Level 7
Thank you so much Peter!
I am really grateful for your kind support 🙂
PeterClemmensen
Tourmaline | Level 20

No problem 🙂

AndreaVianello
Obsidian | Level 7

 

data have;
input GVKey $ Month $ Announcements $ y_n;
infile datalines dlm = ',';
datalines;
1001,Jan, ,0
1001,Feb, ,0
1001,Mar,Mar,1
1001,Apr, ,0
1001,May, ,0
1001,Jun,Jun,1
1001,Jul, ,0
1001,Aug, ,0
1001,Sep,Sep,1
1001,Oct, ,0
1001,Nov, ,0
1001,Dec,Dec,1
1002,Jan, ,0
1002,Feb,Feb,1
1002,Mar, ,0
1002,Apr, ,0
1002,May, ,0
1002,Jun,Jun,1
1002,Jul, ,0
1002,Aug, ,0
1002,Sep,Sep,1
1002,Oct, ,0
1002,Nov,Nov,1
1002,Dec, ,0
; run;

 

 

data want; set have;
retain Ann_in_last_12_months 0;
if y_n=1 then Ann_in_last_12_months+1;
run;

 

bye 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 4 replies
  • 753 views
  • 1 like
  • 3 in conversation