BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
InêsMaximiano
Obsidian | Level 7

data have;
input UserID Year Month Flag;
datalines;

UserIDYearMonthFlag
1202180
1202191
1202211
1202220
1202231
2202011
2202020
2202031
2202070
3202210
3202221
3202230
3202240
3202251

;

 

I want to create a new column that returns 0 if Flag = 1 and that for the other rows it returns the number of months that have passed since the last time that Flag was 1. 

 

Like in the following exemple:

 

UserIDYearMonthFlagColumn wanted
1202180.
12021910
12022110
12022201
12022310
22020110
22020201
22020310
22020704
3202210.
32022210
32022301
32022402
32022510
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

One way:

data have;
   input UserID	Year	Month	Flag;
datalines;
1	2021	8	0
1	2021	9	1
1	2022	1	1
1	2022	2	0
1	2022	3	1
2	2020	1	1
2	2020	2	0
2	2020	3	1
2	2020	7	0
3	2022	1	0
3	2022	2	1
3	2022	3	0
3	2022	4	0
3	2022	5	1
;

data want;
   set have;
   by userid;
   retain flagdate;
   if first.userid then do;
       wanted=.;
       flagdate=.;
   end;
   if flag=1 then do;
      wanted=0;
      flagdate=mdy(month,1,year);
   end;
   else do;
      date=mdy(month,1,year);
      if flagdate then wanted= intck('month',flagdate,date);
   end;
   drop flagdate date;
run;

By group processing creates automatic variables First and Last for each variable on the by statement indicating if the current value is first or last value of a group and can be used to reset values as shown.

Retain keeps values of a variable across data step boundaries for later use.

MDY function to create actual date values for use with the INTCK function which returns counts of intervals.

 

Note use of data step to provide example data. Please try to do that and paste into a text box opened on the forum with the </> icon. Other text may end up with content issues depending on source and paste behavior of the forum.

View solution in original post

2 REPLIES 2
ballardw
Super User

One way:

data have;
   input UserID	Year	Month	Flag;
datalines;
1	2021	8	0
1	2021	9	1
1	2022	1	1
1	2022	2	0
1	2022	3	1
2	2020	1	1
2	2020	2	0
2	2020	3	1
2	2020	7	0
3	2022	1	0
3	2022	2	1
3	2022	3	0
3	2022	4	0
3	2022	5	1
;

data want;
   set have;
   by userid;
   retain flagdate;
   if first.userid then do;
       wanted=.;
       flagdate=.;
   end;
   if flag=1 then do;
      wanted=0;
      flagdate=mdy(month,1,year);
   end;
   else do;
      date=mdy(month,1,year);
      if flagdate then wanted= intck('month',flagdate,date);
   end;
   drop flagdate date;
run;

By group processing creates automatic variables First and Last for each variable on the by statement indicating if the current value is first or last value of a group and can be used to reset values as shown.

Retain keeps values of a variable across data step boundaries for later use.

MDY function to create actual date values for use with the INTCK function which returns counts of intervals.

 

Note use of data step to provide example data. Please try to do that and paste into a text box opened on the forum with the </> icon. Other text may end up with content issues depending on source and paste behavior of the forum.

InêsMaximiano
Obsidian | Level 7
Thank you very much! That really helped!

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 585 views
  • 1 like
  • 2 in conversation