BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Vidhee
Calcite | Level 5

Hello, 

 

I want to add rows based on 2 conditions.

data have-

IdOpen DateClose DateIndicator
123Apr-22Jun-220
456Apr-22Apr-220
789Apr-22Apr-221

 

Here add rows from open date to close date when Open date < Close date. Another condition when Open date = Close date and Indicator = 1 then add rows till today.

Data want - 

IdOpen DateClose DateIndicatorDatecounter
123Apr-22Jun-220Apr-22
123Apr-22Jun-220May-22
123Apr-22Jun-220Jun-22
456Apr-22Apr-220Apr-22
789Apr-22Apr-221Apr-22
789Apr-22Apr-221May-22
789Apr-22Apr-221Jun-22
789Apr-22Apr-221Jul-22

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Are your "dates" actually SAS date values or just character values impersonating dates?

You provide instructions for Open date= Close date AND indicator=1. What about Open date=Close date and Indicator = 0 (or missing)? When you start writing exceptions you should be very clear to state them. Listing an example is not always sufficient.

 

My take:

data have;
   input Id	OpenDate :anydtdte.	CloseDate :anydtdte. Indicator;
   format opendate closedate monyy6.;
datalines;
123	Apr-22	Jun-22	0
456	Apr-22	Apr-22	0
789	Apr-22	Apr-22	1
;

data want;
   set have;
   datecounter=opendate;
   if indicator ne 1 then do;
      do until (datecounter gt closedate);
         output;
         datecounter = intnx('month',datecounter,1);
      end;
   end;
   else if opendate=closedate then do until (datecounter gt today());
         output;
         datecounter = intnx('month',datecounter,1);
   end;

   format datecounter monyy6.;
run;

Personally I believe anyone actually using 2-digit years deserves what eventually comes to them. Look up Y2K.

 


@Vidhee wrote:

Hello, 

 

I want to add rows based on 2 conditions.

data have-

Id Open Date Close Date Indicator
123 Apr-22 Jun-22 0
456 Apr-22 Apr-22 0
789 Apr-22 Apr-22 1

 

Here add rows from open date to close date when Open date < Close date. Another condition when Open date = Close date and Indicator = 1 then add rows till today.

Data want - 

Id Open Date Close Date Indicator Datecounter
123 Apr-22 Jun-22 0 Apr-22
123 Apr-22 Jun-22 0 May-22
123 Apr-22 Jun-22 0 Jun-22
456 Apr-22 Apr-22 0 Apr-22
789 Apr-22 Apr-22 1 Apr-22
789 Apr-22 Apr-22 1 May-22
789 Apr-22 Apr-22 1 Jun-22
789 Apr-22 Apr-22 1 Jul-22

 

Thanks in advance.


 

 

 

 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

UNTESTED CODE

 

data want;
    set have;
    if indicator=0 then n_months=intck('month',opendate,closedate);
    else if indicator=1 then n_months=intck('month',opendate,today());
    do i=0 to n_months;
         datecounter=intnx('month',opendate,i,'b');
         output;
    end;
    format datecounter monyy7.;
    drop i;
run;

 

If you want tested code, do not provide data as screen captures or Excel files. Data must be provided as working SAS data step code, and not in any other format — please test the code to make sure it works before posting it.

 

Also, the above code assumes that opendate and closedate are actual numeric SAS date values, which you did not say they were, and is another reason why the code is untested.

--
Paige Miller
ballardw
Super User

Are your "dates" actually SAS date values or just character values impersonating dates?

You provide instructions for Open date= Close date AND indicator=1. What about Open date=Close date and Indicator = 0 (or missing)? When you start writing exceptions you should be very clear to state them. Listing an example is not always sufficient.

 

My take:

data have;
   input Id	OpenDate :anydtdte.	CloseDate :anydtdte. Indicator;
   format opendate closedate monyy6.;
datalines;
123	Apr-22	Jun-22	0
456	Apr-22	Apr-22	0
789	Apr-22	Apr-22	1
;

data want;
   set have;
   datecounter=opendate;
   if indicator ne 1 then do;
      do until (datecounter gt closedate);
         output;
         datecounter = intnx('month',datecounter,1);
      end;
   end;
   else if opendate=closedate then do until (datecounter gt today());
         output;
         datecounter = intnx('month',datecounter,1);
   end;

   format datecounter monyy6.;
run;

Personally I believe anyone actually using 2-digit years deserves what eventually comes to them. Look up Y2K.

 


@Vidhee wrote:

Hello, 

 

I want to add rows based on 2 conditions.

data have-

Id Open Date Close Date Indicator
123 Apr-22 Jun-22 0
456 Apr-22 Apr-22 0
789 Apr-22 Apr-22 1

 

Here add rows from open date to close date when Open date < Close date. Another condition when Open date = Close date and Indicator = 1 then add rows till today.

Data want - 

Id Open Date Close Date Indicator Datecounter
123 Apr-22 Jun-22 0 Apr-22
123 Apr-22 Jun-22 0 May-22
123 Apr-22 Jun-22 0 Jun-22
456 Apr-22 Apr-22 0 Apr-22
789 Apr-22 Apr-22 1 Apr-22
789 Apr-22 Apr-22 1 May-22
789 Apr-22 Apr-22 1 Jun-22
789 Apr-22 Apr-22 1 Jul-22

 

Thanks in advance.


 

 

 

 

mkeintz
PROC Star

AS @ballardw asked, you haven't said what you want when opendate=closedate and indicator=0.

 

 

Absent that howerver:

 

data have;
   input Id	OpenDate :anydtdte.	CloseDate :anydtdte. Indicator;
   format opendate closedate monyy6.;
datalines;
123	Apr-22	Jun-22	0
456	Apr-22	Apr-22	0
789	Apr-22	Apr-22	1
run;

data want;
  set have;
  do datecounter=opendate by 0 while (datecounter<=ifn(indicator=1 and opendate=closedate,today(),closedate));
    output;
    datecounter=intnx('month',datecounter,1);
  end;
  format datecounter date9. ;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
data have;
infile cards expandtabs;
input Id OpenDate :monyy7. CloseDate :monyy7. Indicator;
format OpenDate CloseDate monyy7.;
cards;
123 Apr-22 Jun-22 0
456 Apr-22 Apr-22 0
789 Apr-22 Apr-22 1
;

data want;
 set have;
 do date=OpenDate to CloseDate;
   if month ne month(date) then output;
   month=month(date);
 end;

 if Indicator=1 and OpenDate=CloseDate then do;
   do date=OpenDate to date();
     if month ne month(date) then output;
     month=month(date);
   end;
 end;
 format date monyy7.;
 drop month;
run;

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 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
  • 4 replies
  • 608 views
  • 3 likes
  • 5 in conversation