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!
@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.
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.
@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 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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.