BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Q1983
Lapis Lazuli | Level 10
data have;
date1='01jan2021'd;
date2='01jan2021'd;
date3='31mar2021'd;
format date1 date2 date3 date9.;
yr_date1 = put(date1,monyy7.);
yr_date2 = put(date2,monyy7.);
yr_date3 = put(date3,monyy7.);
if yr_date1 <>yr_date2 then date2 = .;
if yr_date1 <>yr_date3 then date3 = .;
run;

I have 3 dates.  Using date1 as the base, If the month and year are a match in date1 and date2 I want to show date2.  Same for the relationship between date1 and date3.  The only way I knew to handle was to convert the dates to string (ie date1 put statement).  if yr_date1 <>yr_date2 then date2 = .; if yr_date1 <>yr_date3 then date3 = .;  In this case date1 and date3 are not a match so I want date3 to show a blank.  However a value still shows for date3. 

1 ACCEPTED SOLUTION

Accepted Solutions
jimbarbour
Meteorite | Level 14

How about something like this:

data have;
	date1='01jan2021'd;
	date2='01jan2021'd;
	date3='31mar2021'd;
	format date1 date2 date3 date9.;
run;

data	want;
	DROP	_:;
	set have;
	_Year_Mon1	=	(YEAR(date1) * 100) + MONTH(date1);
	_Year_Mon2	=	(YEAR(date2) * 100) + MONTH(date2);
	_Year_Mon3	=	(YEAR(date3) * 100) + MONTH(date3);

	IF	_Year_Mon2	^=	_Year_Mon1	THEN
		CALL	MISSING(Date2);

	IF	_Year_Mon3	^=	_Year_Mon1	THEN
		CALL	MISSING(Date3);
run;

 

If you want to do this with a larger dataset, there would probably need to be some adjustments, but the YEAR() and MONTH() functions should still do what you would need.  If there were more dates per record than 3, it might be worth converting the code to use an array.

 

Jim

View solution in original post

1 REPLY 1
jimbarbour
Meteorite | Level 14

How about something like this:

data have;
	date1='01jan2021'd;
	date2='01jan2021'd;
	date3='31mar2021'd;
	format date1 date2 date3 date9.;
run;

data	want;
	DROP	_:;
	set have;
	_Year_Mon1	=	(YEAR(date1) * 100) + MONTH(date1);
	_Year_Mon2	=	(YEAR(date2) * 100) + MONTH(date2);
	_Year_Mon3	=	(YEAR(date3) * 100) + MONTH(date3);

	IF	_Year_Mon2	^=	_Year_Mon1	THEN
		CALL	MISSING(Date2);

	IF	_Year_Mon3	^=	_Year_Mon1	THEN
		CALL	MISSING(Date3);
run;

 

If you want to do this with a larger dataset, there would probably need to be some adjustments, but the YEAR() and MONTH() functions should still do what you would need.  If there were more dates per record than 3, it might be worth converting the code to use an array.

 

Jim

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!

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
  • 1 reply
  • 375 views
  • 1 like
  • 2 in conversation