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

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 724 views
  • 0 likes
  • 4 in conversation