BookmarkSubscribeRSS Feed
imdickson
Quartz | Level 8

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?

3 REPLIES 3
Kurt_Bremser
Super User

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;
soham_sas
Quartz | Level 8

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;

art297
Opal | Level 21

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

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1325 views
  • 0 likes
  • 4 in conversation