BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
grace999
Obsidian | Level 7

Hi I would like to create a date field based on the first date from the start_date field in the have data.  

The website does not allow me to paste the code here. I inserted the picture here instead. Thank you very much! 

DATE.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@grace999 wrote:

Note: the number of days is different month by month. For example, February has 29 days this year, March has 31 days and April has 30 days. so the ending date would be different based on which year it is in. 


July has 31 days as well. So the rule, not actually stated, is to have the date to end of the given month?

 

data example;
   input no :$3. date :date9.;
   format date date9.;
   do date=date to intnx('month',date,0,'e');
      output;
   end;
datalines;
c1  25jul2020
c23 27may2020
;

The Do loop is the important part. I used the same variable I read in but you could use date=startdate at the beginning.

The INTNX function is used to increment dates, so the 0 parameter means, "this month" and the 'E' means "end of the month. The output explicitly writes to the output set one time for each iteration through the loop.

View solution in original post

5 REPLIES 5
grace999
Obsidian | Level 7

Note: the number of days is different month by month. For example, February has 29 days this year, March has 31 days and April has 30 days. so the ending date would be different based on which year it is in. 

ballardw
Super User

@grace999 wrote:

Note: the number of days is different month by month. For example, February has 29 days this year, March has 31 days and April has 30 days. so the ending date would be different based on which year it is in. 


July has 31 days as well. So the rule, not actually stated, is to have the date to end of the given month?

 

data example;
   input no :$3. date :date9.;
   format date date9.;
   do date=date to intnx('month',date,0,'e');
      output;
   end;
datalines;
c1  25jul2020
c23 27may2020
;

The Do loop is the important part. I used the same variable I read in but you could use date=startdate at the beginning.

The INTNX function is used to increment dates, so the 0 parameter means, "this month" and the 'E' means "end of the month. The output explicitly writes to the output set one time for each iteration through the loop.

grace999
Obsidian | Level 7
Thank you so much for your quick response. If I want to end at certain month, EX July 30th. What should I do?
grace999
Obsidian | Level 7
sorry typo. The end of July should be July 31st. 2020
ballardw
Super User

@grace999 wrote:
Thank you so much for your quick response. If I want to end at certain month, EX July 30th. What should I do?

I think that you need to provide a clearer example of what this means.

The key, whenever you want something like this to end is to provide the target date as the "to " value in the do loop.

 

If you wanted to have all of the sequences end with a specific date then you would provide the date. That would look like

 

do date= date to "15OCT2020"d ;  

the d following a quoted value in date9 format is a literal SAS date value.

 

If you need something else then you need to provide rules based on the values in the data that can be applied. The INTNX function has some moderately complex options besides the basic interval list. read the documentation.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 1367 views
  • 2 likes
  • 2 in conversation