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

Hi,

I am trying to find  if a date in a variable falls under Month1,Month2, Month3,Month4....Month6 category using a do loop between startdt and incremented startdate   i.e., Startdate+30. 

 

 

By running the below logic, I get a value for the first record as  Month2 and a value for rest of the Months too which is not correct. How can i fix this logic?

 

 

dataset--

 

start                 vardt                  enddt

01Jan2017   11Mar2017      01Jan2018

01Jan2017   02May2017     01Jan2018

01Jan2017   11Apr2017       01Jan2018

 


data test2;
set test1;


mindt=startdt;
maxdt=enddt;


array Months(6);

Mnth=30;
start=mindt;

do i = 1 to Months;

mnth=mnth+30;

stopdt=maxdt;
if startdt <= vardt<=startdt+mnth then Months(i)=1;

end;
start=start+mnth;

drop  i;
run;

 

 

 

Thank you in advance.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
DanielLangley
Quartz | Level 8
data test1;
input start DATE9. vardt DATE9. enddt DATE9.;
FORMAT start Date9. vardt Date9. enddt Date9.;
DATALINES;
1jan2017 11Mar2017 1jan2018
1jan2017 02May2017 1jan2018
1jan2017 11Apr2017 1jan2018
;
run;

Data test2 (Drop = i);
set test1;

ARRAY Months{6};
DO i = 1 to DIM(Months);
	IF vardt >= Start + (30*(i-1)) AND vardt <= Start + (30*i) THEN Months{i}=1;
END;
RUN;

Try this.

View solution in original post

2 REPLIES 2
DanielLangley
Quartz | Level 8
data test1;
input start DATE9. vardt DATE9. enddt DATE9.;
FORMAT start Date9. vardt Date9. enddt Date9.;
DATALINES;
1jan2017 11Mar2017 1jan2018
1jan2017 02May2017 1jan2018
1jan2017 11Apr2017 1jan2018
;
run;

Data test2 (Drop = i);
set test1;

ARRAY Months{6};
DO i = 1 to DIM(Months);
	IF vardt >= Start + (30*(i-1)) AND vardt <= Start + (30*i) THEN Months{i}=1;
END;
RUN;

Try this.

new510
Fluorite | Level 6

Thank you Daniel.

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
  • 2 replies
  • 873 views
  • 0 likes
  • 2 in conversation