DATA Step, Macro, Functions and more

Loop a valid calendar date

Reply
Frequent Contributor
Posts: 83

Loop a valid calendar date

Hi Guys, I want to create a loop, to get the value of date in the entire pre-defined year. For example, if i pre-define 2018, in the output, i want to get 20180101 until 20181231. So if i pre-define 2019, i will get the output result set of 20190101 until 20191231

 

Then later I will use the result sets to further process my other data. May i know if there is a function to achieve the task?

Super User
Posts: 9,610

Re: Loop a valid calendar date

Posted in reply to imdickson

Since SAS date values are counts of days, this can easily be achieved in a do loop:

%let year=2019;

data want;
do mydate = mdy(1,1,&year) to mdy(12,31,&year);
  /* your code */
end;
format mydate yymmddn8.;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 39

Re: Loop a valid calendar date

Posted in reply to imdickson

hi , you can do it using a do loop also , please refer the below code 

 

data dat;
_date=mdy(1,1,&year);
output;
do until (_date=mdy(12,31,&year));
_date+1;
output;
end;
format _date date9.;
run;

PROC Star
Posts: 8,115

Re: Loop a valid calendar date

Posted in reply to imdickson

Not sure how you want to use it, but the year function may be all that you need. e.g.:

data have;
  input date date9.;
  format date date9.;
  cards;
10jan2017
5mar2018
10jun2018
14may2019
;

data want;
  set have (where=(year(date) eq 2018));
run;

Art, CEO, AnalystFinder.com

 

Ask a Question
Discussion stats
  • 3 replies
  • 90 views
  • 0 likes
  • 4 in conversation