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

Hello pals,

 

I have a file in which i have Datex and date 1 ,date2,date3 and date4 columns and my requirements is to compare 'Datex' column with other 4 date columns.

 

If datex = 2019-01-01 

Date1=2016-01=01  Date2=2019-01=01 Date3 = 2017-09-01

Date4 = 2018-11-01

 

Then my requirement is to check datex column value with all other date columns and then if any of these date values is less than 30 days or greater than 30 days or falls with this range ..then the date1 column should be That valid date which falls in these category ...in the above case date 2 column falls in these category and it should be in Date1 column..

 

Thanks much in advance.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Array processing:

data have;
input datex :yymmdd10. (date1-date4) (:yymmdd10.);
format datex date1-date4 yymmddd10.;
datalines;
2019-01-01 2016-01-01 2019-01-01 2017-09-01 2018-11-01
;
run;

data want;
set have;
array dates {*} date1-date4;
do i = 1 to dim(dates);
  if datex - 30 <= dates{i} <= datex + 30
  then do;
    date1 = dates{i};
    leave;
  end;
end;
drop i;
run;

proc print data=want noobs;
run;

Result:

     datex         date1         date2         date3         date4

2019-01-01    2019-01-01    2019-01-01    2017-09-01    2018-11-01

date1 has been replaced with the value of date2.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Array processing:

data have;
input datex :yymmdd10. (date1-date4) (:yymmdd10.);
format datex date1-date4 yymmddd10.;
datalines;
2019-01-01 2016-01-01 2019-01-01 2017-09-01 2018-11-01
;
run;

data want;
set have;
array dates {*} date1-date4;
do i = 1 to dim(dates);
  if datex - 30 <= dates{i} <= datex + 30
  then do;
    date1 = dates{i};
    leave;
  end;
end;
drop i;
run;

proc print data=want noobs;
run;

Result:

     datex         date1         date2         date3         date4

2019-01-01    2019-01-01    2019-01-01    2017-09-01    2018-11-01

date1 has been replaced with the value of date2.

ballardw
Super User

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

 

Your pseudo code


@Codepro wrote:

 

If datex = 2019-01-01 

Date1=2016-01=01  Date2=2019-01=01 Date3 = 2017-09-01

Date4 = 2018-11-01

 


Contains several errors. First date literals, or specific date values you are interested in are in the form of '01JAN2019'd. Second 2019-01-01 is not a valid value though SAS may do arithmetic with it yielding 2017. If your value is a character variable then the comparison would be datex='2019-01-01'; if it is supposed to be an actual SAS date value then datex='01JAN2019'd.

 

Your if should have a "then do" to indicate the following statements are conditional.

The statements in SAS must end in a ;

You would also need an "end;" after the conditional assignments to end the block.

Codepro
Fluorite | Level 6
Thanks a lot for all your help👍

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 3 replies
  • 1050 views
  • 2 likes
  • 3 in conversation