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

Hi friends ,

I was wanting some help on the below  . I need to compare the Service_Dates and if the Service date is present in the above record create an Flag='Y'.

My input data is attached as an excel 

 

dennis_oz_0-1622516642887.png

 

Alternatively, the input code in a data step:

DATA WORK.check3;
     INPUT
        id $1-8  SERVICE_DATES $10-69 cntr $70-72 ;
		datalines;
267005668 20210401 || 20210401 || 20210401 || 20210401 || 20210401    3
267005668 20210401 || 20210401 || 20210401 || 20210401	              3
245060165 20200512 || 20200512	                                      9
245060165 20191212 || 20191212 || 20191212 || 20191212 || 20191212	  9
241777762 20210407                                                    10
241777762 20210407 || 20210407 || 20210407 || 20210407	              10
RUN;

Please can you help with my query .

 

Kind Regards.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
DATA WORK.check3;
     INPUT
        id $1-8  SERVICE_DATES $10-69 cntr $70-72 ;
		datalines;
267005668 20210401 || 20210401 || 20210401 || 20210401 || 20210401    3
267005668 20210401 || 20210401 || 20210401 || 20210401	              3
245060165 20200512 || 20200512	                                      9
245060165 20191212 || 20191212 || 20191212 || 20191212 || 20191212	  9
241777762 20210407                                                    10
241777762 20210407 || 20210407 || 20210407 || 20210407	              10
;
RUN;

data want;
 set check3;
 lag=lag(SERVICE_DATES);
 if id=lag(id) then do;
   flag='Y';
   do i=1 to countw(SERVICE_DATES,'|');
     temp=scan(SERVICE_DATES,i,'|');
	 if not find(lag,strip(temp)) then do;
       flag=' ';leave;
	 end;
   end;
 end;
drop lag temp i;
run;

View solution in original post

4 REPLIES 4
mkeintz
PROC Star

Do you always have non-overlapping pairs of records to compare?  I.e., do you only compare record 2 to 1, 4 to 3, 6 to 5, but not 3 to 2, 5 to 4, etc.

 

Also why are you reading all your date variables as one long string?  Why not read each date value as a separate variable?

 

And finally, it seems that within each record containing multiple dates, those dates are always the same.  Is that the case for your entire dataset?

--------------------------
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

--------------------------
dennis_oz
Quartz | Level 8

hi @mkeintz  ,

this is the way I am receiving the data . 

I have further wrangled the data into below . I want to check now if a value in service_dates occurs in service_dates_new . If so just create a new column flag .

Please see my desied output in the last screen shot under output 

 

dennis_oz_1-1622527206539.png

 

input code 

DATA WORK.check3;
     INPUT
        id $1-9  SERVICE_DATES $10-69 cntr $70-72  SERVICE_DATES_NEW $74-130;
		datalines;
267005668 20210401 || 20210401 || 20210401 || 20210423 || 20210401    1   20210423 
245060165 20200512 || 20200512	                                      2   20191212 || 20191212 || 20191212 || 20191212 || 20191212
241777762 20210407                                                    3  20210407 || 20210407 || 20210407 || 20210407	
RUN;

output

======

dennis_oz_2-1622527565086.png

 

Ksharp
Super User
DATA WORK.check3;
     INPUT
        id $1-8  SERVICE_DATES $10-69 cntr $70-72 ;
		datalines;
267005668 20210401 || 20210401 || 20210401 || 20210401 || 20210401    3
267005668 20210401 || 20210401 || 20210401 || 20210401	              3
245060165 20200512 || 20200512	                                      9
245060165 20191212 || 20191212 || 20191212 || 20191212 || 20191212	  9
241777762 20210407                                                    10
241777762 20210407 || 20210407 || 20210407 || 20210407	              10
;
RUN;

data want;
 set check3;
 lag=lag(SERVICE_DATES);
 if id=lag(id) then do;
   flag='Y';
   do i=1 to countw(SERVICE_DATES,'|');
     temp=scan(SERVICE_DATES,i,'|');
	 if not find(lag,strip(temp)) then do;
       flag=' ';leave;
	 end;
   end;
 end;
drop lag temp i;
run;
dennis_oz
Quartz | Level 8
Many Thanks !! Your code works perfectly 🙂 !

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 731 views
  • 1 like
  • 3 in conversation