DATA Step, Macro, Functions and more

Do loop

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Do loop

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.

 

 


Accepted Solutions
Solution
a month ago
Occasional Contributor
Posts: 10

Re: Do loop

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


All Replies
Solution
a month ago
Occasional Contributor
Posts: 10

Re: Do loop

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.

Occasional Contributor
Posts: 10

Re: Do loop

Posted in reply to DanielLangley

Thank you Daniel.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 147 views
  • 0 likes
  • 2 in conversation