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

Hello, 

 

I want to assign the same start and end date by matching variable. Any advice?

 

data have;
	input id $ matching $ start :mmddyy10. end :mmddyy10.;
	format	start mmddyy10.
			end mmddyy10.;
	datalines;
		B0000362004	A 01/01/2001 11/11/2011
		B0005852005 A . . 
		B0009422005 B 02/02/2002 12/12/2012
		B1370912010 B . .
	;
run;
data want;
	input id $ matching $ start :mmddyy10. end :mmddyy10.;
	format	start mmddyy10.
			end mmddyy10.;
	datalines;
		B0000362004	A 01/01/2001 11/11/2011
		B0005852005 A 01/01/2001 11/11/2011 
		B0009422005 B 02/02/2002 12/12/2012
		B1370912010 B 02/02/2002 12/12/2012
	;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;
	input id $ matching $ start :mmddyy10. end :mmddyy10.;
	format	start mmddyy10.
			end mmddyy10.;
	datalines;
		B0000362004	A 01/01/2001 11/11/2011
		B0005852005 A . . 
		B0009422005 B 02/02/2002 12/12/2012
		B1370912010 B . .
	;
run;

data want;
 update have(obs=0) have;
 by  matching;
 output;
run;

View solution in original post

2 REPLIES 2
ballardw
Super User

This seems to work for your example data.

data want;
   set have;
   retain lstart lend;
   lstart = coalesce(start,lstart);
   lend   = coalesce(end,lend);
   start = coalesce(start,lstart);
   end   = coalesce(end,lend);
   drop lstart lend;
run;

Retain will keep values across records. The Coalesce (and Coalescec for character values) returns the first non-missing value in the list and is basically used to avoid a bunch of similar "if missing(var) then var=varb;" type statements.

 

Note If you do NOT actually want to carry the dates forward with the ID changes then you need to state so and indicate what should be done. The above code ignores the ID.

novinosrin
Tourmaline | Level 20

data have;
	input id $ matching $ start :mmddyy10. end :mmddyy10.;
	format	start mmddyy10.
			end mmddyy10.;
	datalines;
		B0000362004	A 01/01/2001 11/11/2011
		B0005852005 A . . 
		B0009422005 B 02/02/2002 12/12/2012
		B1370912010 B . .
	;
run;

data want;
 update have(obs=0) have;
 by  matching;
 output;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 706 views
  • 3 likes
  • 3 in conversation