BookmarkSubscribeRSS Feed
ridi
Calcite | Level 5

Hello, I have a dataset where I am trying to fill in the date value using values in another column . The condition I am trying to apply is for each ID, if any one row is blank then leave blank. Fill in date value if all rows has date. Can you please help with the sas code.

Here is the variable attribute and table I have and want. There are other variables as well in the actual dataset.

VariableTypeLenFormatInformat
IDNum8BEST. 
End DateNum8MMDDYY10.MMDDYY10.

 

Problem 3
Data: Have3 Data: Want3
IDEnd Date IDEnd DateEnd Date1
1  1  
112/24/2023 112/24/2023 
1     
112/24/2023 112/24/2023 
21/13/2005 21/13/2005 
21/13/2005 21/13/2005 
2  2  
3  3  
33/5/2017 33/5/2017 
3  3  
3  3  
3  3  
4  4  
4  4  
4  4  
55/5/2020 55/5/20205/5/2020
55/5/2020 55/5/20205/5/2020
611/1/2024 611/1/202411/1/2024

Thank you.

 

6 REPLIES 6
PaigeMiller
Diamond | Level 26
/* UNTESTED CODE */
proc summary data=have3 nway;
    class id;
    var enddate;
    output out=counts nmiss=nmiss;
run;

data want3;
    merge have3 counts(drop=_type_ _freq_);
    by id;
    if nmiss=0 then enddate1=enddate;
run;

 

The code above is UNTESTED since your data is not in a usable form. From now on, please provide data as text in working SAS data step code (examples and instructions). We cannot work with Excel files, screen captures, or copy/paste from Excel, 

--
Paige Miller
Patrick
Opal | Level 21

@PaigeMiller I started to copy/paste data provided as tables into Copilot asking for a SAS data step. This often works quite well.

PaigeMiller
Diamond | Level 26

@Patrick wrote:

@PaigeMiller I started to copy/paste data provided as tables into Copilot asking for a SAS data step. This often works quite well.


In my opinion, it is the responsibility of the original poster to provide data in usable form if they want code that works; it is not my responsibility to convert it.

--
Paige Miller
Patrick
Opal | Level 21

Below should work. And yes, going forward please try and provide sample data in directly usable form similar to below data step Have. This helps us help you.

data have;
	infile datalines truncover dsd dlm=' ';
	input ID : 8. End_Date_have : mmddyy10. End_Date_want : mmddyy10.;
	format End_Date_have End_Date_want date9.;
	datalines;
1
1 12/24/2023
1
1 12/24/2023
2 1/13/2005 
2 1/13/2005
2
3
3 3/5/2017 
3
3
3
4
4
4 
5 5/5/2020 5/5/2020
5 5/5/2020 5/5/2020
6 11/1/2024 11/1/2024
;
run;

data want;
	if _n_=1 then
		do;
			dcl hash h1(dataset:'have(where=(missing(end_date_have)))');
			h1.defineKey('id');
			h1.defineDone();
		end;
	set have;
	if h1.check() ne 0 then end_date_derived=end_date_have;
	format end_date_derived date9.;
run;

proc print data=want;
run;
Ksharp
Super User

Using Patrick's data step to get table, and WANT variable is what you are looking for .

 

data have;
	infile datalines truncover dsd dlm=' ';
	input ID : 8. End_Date_have : mmddyy10. End_Date_want : mmddyy10.;
	format End_Date_have End_Date_want date9.;
	datalines;
1
1 12/24/2023
1
1 12/24/2023
2 1/13/2005 
2 1/13/2005
2
3
3 3/5/2017 
3
3
3
4
4
4 
5 5/5/2020 5/5/2020
5 5/5/2020 5/5/2020
6 11/1/2024 11/1/2024
;
run;

proc sql;
create table want as
select *,case when count(*)=n(End_Date_have) then End_Date_have else . end as want format=date9.
 from have 
  group by id;
quit;
mkeintz
PROC Star

Your data are sorted by ID.  You want new variable END_DATE1 to take the value of END_DATE only for ID's than have no instance of missing END_DATE, correct?

 

If so, then

 

data want;
  merge have (where=(end_date=.)  in=inblank)
        have;
  by id;
  format end_date1 date9.  ;
  if inblank=0 then end_date1=end_date;
run;

The temporary dummy variable INBLANK will be zero only for those ID's with no rows with missing END_DATE.  

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

--------------------------

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 329 views
  • 0 likes
  • 5 in conversation