简体   繁体   中英

SAS DO LOOP with specific dates

I want to create a data set where I only want to keep 5 specific dates. So my &date is 31mar2020 and &enddate is 31mar2025 and I only want to keep 31mar every year until 2025.

With my code below it creates dates for everyday up to 31mar2025 and thats to much so I only want to keep 5 specific dates.

How can i do that?

Thank you

  DATA LOOP;FORMAT ROLL_BASE_DT DATE9.;DO  ROLL_BASE_DT =&DATE TO &ENDdate;OUTPUT;END;RUN;

enter code here

enter code here

You can use commas in the DO statement to list multiple values.

do date='31mar2021'd,'31mar2022'd,'31mar2023'd,'31mar2024'd,'31mar2025'd;
  ...
end;

You could loop over the YEAR value instead.

do year=2021 to 2025;
   date=mdy(3,31,year);
  ...
end;

You could use INTNX() to increment the date by YEAR. You can use INTCK() to figure out how many times to run the loop.

do index=0 to intck('year',&DATE,&ENDdate);
  date=intnx('year',&date,index,'s');
  ...
end;

If it's just the 5 dates you want, you could use the cards input (I know of it but have never used it personally).

Alternatively, rather than using a loop just set the values individually with the output keyword after each time you set the value. That should do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM