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

Hihi, anyone here understand and could share with me the solutions to resolve the below queries? Smiley Very Happy

 

1) How to assign the number to the first "1" identified under "Active" variable by "Customer ID"?

2) How to produce the "Desired result" by identifying the first "1" and creating the column of "Desired Result" that captures the preceding 12 months observations?

 

Thank you so much for shedding the light for these ❤️

 

 

 

 

PeriodCustomer IDActiveSelf-created NumberDesired Result
20130410 1
20130510 1
20130610 1
20130710 1
20130810 1
20130910 1
20131011121
20131110  
20131210  
20140110  
20140210  
20140310  
20140410  
20140510  
20140610  
20140710  
20140811  
20140910  
20141010  
20141110  
20141210  
20130480  
20130580  
20130680  
20130780 1
20130880 1
20130980 1
20131080 1
20131180 1
20131280 1
20140180 1
20140280 1
20140380 1
20140480 1
20140580 1
20140681121
20140781  
20140881  
20140980  
20141080  
20141180  
20141280  
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Ok. This gives you what you want. Ask if you have questions about the code 🙂

 

data have;
input Period :yymmn6. Customer_ID Active;
format Period yymmn6.;
datalines;
201304 1 0
201305 1 0
201306 1 0
201307 1 0
201308 1 0
201309 1 0
201310 1 1
201311 1 0
201312 1 0
201401 1 0
201402 1 0
201403 1 0
201404 1 0
201405 1 0
201406 1 0
201407 1 0
201408 1 1
201409 1 0
201410 1 0
201411 1 0
201412 1 0
201304 8 0
201305 8 0
201306 8 0
201307 8 0
201308 8 0
201309 8 0
201310 8 0
201311 8 0
201312 8 0
201401 8 0
201402 8 0
201403 8 0
201404 8 0
201405 8 0
201406 8 1
201407 8 1
201408 8 1
201409 8 0
201410 8 0
201411 8 0
201412 8 0
;

data want(drop=from to);
   do until (last.Customer_ID);
      set have;
      by Customer_ID;
      if Active=1 and to=. then do;
         to=Period;
         from=intnx('month', Period, -11, 's');
      end;
   end;

   do until (last.Customer_ID);
      set have;
      by Customer_ID;
      Desired_Result=0;
      if from le Period le to then Desired_Result=1;
      output;
   end;
run;

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

So the Self Created Number, could that be other values than 12 or is it always 12?

y3arire_7
Fluorite | Level 6
Hi Draycut, for this scenario, it would be always 12.
PeterClemmensen
Tourmaline | Level 20

Ok. This gives you what you want. Ask if you have questions about the code 🙂

 

data have;
input Period :yymmn6. Customer_ID Active;
format Period yymmn6.;
datalines;
201304 1 0
201305 1 0
201306 1 0
201307 1 0
201308 1 0
201309 1 0
201310 1 1
201311 1 0
201312 1 0
201401 1 0
201402 1 0
201403 1 0
201404 1 0
201405 1 0
201406 1 0
201407 1 0
201408 1 1
201409 1 0
201410 1 0
201411 1 0
201412 1 0
201304 8 0
201305 8 0
201306 8 0
201307 8 0
201308 8 0
201309 8 0
201310 8 0
201311 8 0
201312 8 0
201401 8 0
201402 8 0
201403 8 0
201404 8 0
201405 8 0
201406 8 1
201407 8 1
201408 8 1
201409 8 0
201410 8 0
201411 8 0
201412 8 0
;

data want(drop=from to);
   do until (last.Customer_ID);
      set have;
      by Customer_ID;
      if Active=1 and to=. then do;
         to=Period;
         from=intnx('month', Period, -11, 's');
      end;
   end;

   do until (last.Customer_ID);
      set have;
      by Customer_ID;
      Desired_Result=0;
      if from le Period le to then Desired_Result=1;
      output;
   end;
run;
y3arire_7
Fluorite | Level 6
Hi Thanks for the code. It works; however, it did not assign the figure to only the preceding 12 months but all the preceding months with the first Active = 1 identified 😃
Ksharp
Super User
data have;
input Period :yymmn6. Customer_ID Active;
format Period yymmn6.;
datalines;
201304 1 0
201305 1 0
201306 1 0
201307 1 0
201308 1 0
201309 1 0
201310 1 1
201311 1 0
201312 1 0
201401 1 0
201402 1 0
201403 1 0
201404 1 0
201405 1 0
201406 1 0
201407 1 0
201408 1 1
201409 1 0
201410 1 0
201411 1 0
201412 1 0
201304 8 0
201305 8 0
201306 8 0
201307 8 0
201308 8 0
201309 8 0
201310 8 0
201311 8 0
201312 8 0
201401 8 0
201402 8 0
201403 8 0
201404 8 0
201405 8 0
201406 8 1
201407 8 1
201408 8 1
201409 8 0
201410 8 0
201411 8 0
201412 8 0
;
data want;
 set have;
 by Customer_ID;
 retain want 1 found 0;
 if first.Customer_ID then do; want=1; found=0; end;
 if found then want=0;
 if active=1 and not found then found=1;
 drop found;
run;
y3arire_7
Fluorite | Level 6
Hi Ksharp thanks for the sharing.
This code works; however, it did not assign the figure to only the preceding 12 months but all the preceding months with the first Active = 1 identified 😃

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 7 replies
  • 1858 views
  • 2 likes
  • 3 in conversation