BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ksharp
Super User

Sorry. OK .try this one.

Still hard.

data have;
input ID     year     month     months_continuous     bef_3months     aft_12_months;
cards;
68     2009     1     1     0     0
68     2009     2     2     0     0
68     2009     3     3     0     0
68     2009     4     4     0     0
68     2009     5     5     0     0
68     2009     6     6     0     0
68     2009     7     7     0     0
68     2009     8     8     1     0
68     2009     9     9     1     0
68     2009     10     10     1     0
68     2009     11     11     0     1
68     2010     1     13     0     1
68     2010     2     14     0     1
68     2010     3     15     0     1
68     2010     4     16     0     1
68     2010     5     17     0     1
68     2010     6     18     0     1
68     2010     7     19     0     1
68     2010     8     20     0     1
68     2010     9     21     0     1
68     2010     10     22     0     1
68     2010     11     23     0     1
72     2009     1     1     0     0
72     2009     2     2     0     0
72     2009     3     3     1     0
72     2009     4     4     1     0
72     2009     5     5     1     0
72     2009     6     6     0     1
72     2009     7     7     0     1
72     2009     8     8     0     1
72     2009     9     9     0     1
72     2009     10     10     0     1
72     2009     11     11     0     1
72     2010     1     13     0     1
72     2010     2     14     0     1
72     2010     3     15     0     1
72     2010     4     16     0     1
72     2010     5     17     0     1
72     2010     6     18     0     1
;
run;
proc sort data=have;by  id      months_continuous;run;
data x1(keep=id month rename=(month=_month));
 set have;
 if  bef_3months=0 and lag(bef_3months)=1;
run;
data x2;
 merge have x1;
 by id;
run;
data temp;
 set x2;
 by id;
 retain flag 1;
 if bef_3months = 1 or  aft_12_months = 1 then do;n=months_continuous-_month;output;end;
 drop _month;
run;

proc transpose data=temp out=temp1(drop=_:) prefix=mon;
by   id  months_continuous ;
var flag;
id n;
idlabel n;
run;
data temp2;
 merge have temp1  ;
 by id months_continuous;
run;
proc stdize data=temp2 missing=0 out=want reponly;run;
proc sql noprint;
select translate(cats('mon',label),'_','-') into : list separated by ' '
 from dictionary.columns
  where libname='WORK' and memname='WANT' and label is not missing
   order by input(strip(label),best32.);
quit;

data want;
 retain ID     year     month     months_continuous     bef_3months     aft_12_months
 &list ;
 set want;
run;

Xia Keshan

Message was edited by: xia keshan

Message was edited by: xia keshan Still hard

Jessica98
Calcite | Level 5

Hi ,

the code works brilliantly, i am trying to learn what you have done. I notice there is one small error, let me know what you think about it. For the output that i have obtained below, for ID 68 and & 72 it is "0" for mon1 & mon6 (underlined below) where it should have been "1" . There is one instance like this for all ID's. Any idea why?

IDyearmonthmonths_continuousbef_3monthsaft_12_monthsmon_3mon_2mon_1mon0mon1mon2mon3mon4mon5mon6mon7mon8mon9mon10mon11mon12mon13mon14mon15mon16mon17mon18mon19mon20mon21mon22mon23
6820091100000000000000000000000000000
6820092200000000000000000000000000000
6820093300000000000000000000000000000
6820094400000000000000000000000000000
6820095500000000000000000000000000000
6820096600000000000000000000000000000
6820097700000000000000000000000000000
6820098810100000000000000000000000000
6820099910010000000000000000000000000
682009101010001000000000000000000000000
682009111101000100000000000000000000000
68201011301000001000000000000000000000
68201021401000000100000000000000000000
68201031501000000010000000000000000000
68201041601000000001000000000000000000
68201051701000000000100000000000000000
68201061801000000000010000000000000000
68201071901000000000001000000000000000
68201082001000000000000100000000000000
68201092101000000000000010000000000000
682010102201000000000000001000000000000
682010112301000000000000000100000000000
7220091100000000000000000000000000000
7220092200000000000000000000000000000
7220093310100000000000000000000000000
7220094410010000000000000000000000000
7220095510001000000000000000000000000
7220096601000100000000000000000000000
7220097701000010000000000000000000000
7220098801000001000000000000000000000
7220099901000000100000000000000000000
722009101001000000010000000000000000000
722009111101000000001000000000000000000
72201011301000000000010000000000000000
72201021401000000000001000000000000000
72201031501000000000000100000000000000
72201041601000000000000010000000000000
72201051701000000000000001000000000000
72201061801000000000000000100000000000
Ksharp
Super User

No . it is not error.

Because  that obs's months_continuous is missing . It should be 12 .So for id=72.

And glad it is working .

Xia Keshan

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 25. 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
  • 17 replies
  • 2402 views
  • 6 likes
  • 4 in conversation