DATA Step, Macro, Functions and more

Month beging with 00:00:00 month end with 23:59:59

Reply
Frequent Contributor
Frequent Contributor
Posts: 79

Month beging with 00:00:00 month end with 23:59:59

I have monthly report, customer want date range as following format.

 

example:

 

startdat='01Apr2017 00:00:00';

  enddt ='30apr2017 23:59:59';

 

In order for me to schedule the process, I need be able to aumatically get the date range as above format,  hh:mm:ss part is not change, onlt ddmmyyyy part changes every month. do no tknow how ?

 

 Example:

 

  '01Apr2017 00:00:00'

  '30apr2017 23:59:59'

 

  Red part never change.

 

No clue how to .

 

Thank you

 

 

 

Super User
Posts: 10,500

Re: Month beging with 00:00:00 month end with 23:59:59

Date range or datetime?

You are missing some important pieces. Are there any variables involved? Are the actual SAS DATE values or DATETIME values?

 

How do you currently tell the process which records to use?

 

If I have a SAS DATETIME then something like:

 

If Month(datepart(dtvariable))=4; will select all records where the datetime variable is in April (month=4).

 

If you have date variable then

month(datevar)=4.

 

Or are you asking about displaying values?

Frequent Contributor
Frequent Contributor
Posts: 79

Re: Month beging with 00:00:00 month end with 23:59:59

HI

data _null_;

dEnd =intnx('Month', today(), -1,'e');

dStart =intnx('Month', today(), -1,'b');

Call symput('dmstart',CATS("'",PUT(dStart,DATE9.),"'"))

Call symput('dmEnd',CATS("'",PUT(dEnd,DATE9.),"'"));

 

run;

 

iI want reult :      dmStart 00:00:00

                          dmEnd  23:59:59

Trusted Advisor
Posts: 1,374

Re: Month beging with 00:00:00 month end with 23:59:59

You can use the DHMS() function to create a datetime variable from date, hour, minute, second variables:

 

start_dtime = dhms(dStart,0,0,0);

end_dtime = dhms(dEnd, 23,59,59);

Frequent Contributor
Frequent Contributor
Posts: 79

Re: Month beging with 00:00:00 month end with 23:59:59

Thank you I am goign to try.

Frequent Contributor
Frequent Contributor
Posts: 79

Re: Month beging with 00:00:00 month end with 23:59:59

Peferct it works. Thank you Very much.

Super User
Posts: 10,500

Re: Month beging with 00:00:00 month end with 23:59:59

MUST you have a space beteen the date and time portion?

data _null_; 
   dEnd   =intnx('dtmonth', dhms(today(),0,0,0), -1,'e');
   dStart =intnx('dtMonth', dhms(today(),0,0,0), -1,'b');
   Call symputx('dmstart',PUT(dStart,DATEtime18.));
   Call symputx('dmEnd'  ,PUT(dEnd  ,DATEtime18.));
run; 

Note that symputx strips leading blanks so you don't need that cats function.

 

Frequent Contributor
Frequent Contributor
Posts: 79

Re: Month beging with 00:00:00 month end with 23:59:59

thank you !, going  to try

Frequent Contributor
Frequent Contributor
Posts: 79

Re: Month beging with 00:00:00 month end with 23:59:59

Perfect, it works.

Frequent Contributor
Frequent Contributor
Posts: 79

Re: Month beging with 00:00:00 month end with 23:59:59

If  I need out put as this format:

 

01APR2017:12:00:00 AM 30APR2017:11:59:59 PM

 

How to get...

 

data _null_;

dEnd=intnx('dtmonth', dhms(today(),0,0,0),-1,'e');

dStart=intnx('dtMonth', dhms(today(),0,0,0),-1,'b');

Call symputx('dmstart',PUT(dStart,dateampm.));

Call symputx('dmEnd',PUT(dEnd,dateampm.));

run;

%PUT &dmstart &dmEnd;

 

01APR17:12:00:00 AM 30APR17:11:59:59 PM

 

not as 2017

 

 

Frequent Contributor
Frequent Contributor
Posts: 79

Re: Month beging with 00:00:00 month end with 23:59:59

I need 4 digital of the year.

 

01APR2017:12:00:00 AM 30APR2017:11:59:59 PM

 

this would not work:becasue it has 2 digital of the year.

data _null_;

dEnd=intnx('dtmonth', dhms(today(),0,0,0),-1,'e');

dStart=intnx('dtMonth', dhms(today(),0,0,0),-1,'b');

Call symputx('dmstart',PUT(dStart,dateampm.));

Call symputx('dmEnd',PUT(dEnd,dateampm.));

run;

%PUT &dmstart &dmEnd;

Super User
Posts: 10,500

Re: Month beging with 00:00:00 month end with 23:59:59

You need to look at the format documentation. Almost every format has a default appearance length but options to control more or fewer characters.

 

Use

DATEampm22.

to get enough characters to display the year with 4 digits and time to seconds.

Frequent Contributor
Frequent Contributor
Posts: 79

Re: Month beging with 00:00:00 month end with 23:59:59

data _null_;

dEnd=intnx('dtmonth', dhms(today(),0,0,0),-1,'e');

dStart=intnx('dtMonth', dhms(today(),0,0,0),-1,'b');

Call symputx('dmstart',PUT(dStart,DATEampm22.));

Call symputx('dmEnd',PUT(dEnd,DATEampm22.));

run;

%PUT &dmstart &dmEnd

 

result is:

 

01APR2017:12:00:00 AM 30APR2017:11:59:59 PM

 

Extra in red.  how to remove extra.

 

Super User
Posts: 6,936

Re: Month beging with 00:00:00 month end with 23:59:59


JHE wrote:

data _null_;

dEnd=intnx('dtmonth', dhms(today(),0,0,0),-1,'e');

dStart=intnx('dtMonth', dhms(today(),0,0,0),-1,'b');

Call symputx('dmstart',PUT(dStart,DATEampm22.));

Call symputx('dmEnd',PUT(dEnd,DATEampm22.));

run;

%PUT &dmstart &dmEnd

 

result is:

 

01APR2017:12:00:00 AM 30APR2017:11:59:59 PM

 

Extra in red.  how to remove extra.

 


Store into character variables and use substr() to replace the colon:

data _null_;
dEnd=intnx('dtmonth', dhms(today(),0,0,0),-1,'e');
dStart=intnx('dtMonth', dhms(today(),0,0,0),-1,'b');
dend_char = put(dend,dateampm22.);
substr(dend_char,11,1) = ' ';
dstart_char = put(dstart,dateampm22.);
substr(dstart_char,11,1) = ' ';
call symputx('dmstart',dstart_char);
call symputx('dmEnd',dend_char);
run;

%put &dmstart &dmEnd;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Frequent Contributor
Posts: 79

Re: Month beging with 00:00:00 month end with 23:59:59

agrere, that is the solution!

Ask a Question
Discussion stats
  • 15 replies
  • 163 views
  • 0 likes
  • 4 in conversation