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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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