BookmarkSubscribeRSS Feed
JHE
Obsidian | Level 7 JHE
Obsidian | Level 7

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

 

 

 

15 REPLIES 15
ballardw
Super User

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?

JHE
Obsidian | Level 7 JHE
Obsidian | Level 7

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

Shmuel
Garnet | Level 18

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);

JHE
Obsidian | Level 7 JHE
Obsidian | Level 7

Thank you I am goign to try.

JHE
Obsidian | Level 7 JHE
Obsidian | Level 7

Peferct it works. Thank you Very much.

ballardw
Super User

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.

 

JHE
Obsidian | Level 7 JHE
Obsidian | Level 7

thank you !, going  to try

JHE
Obsidian | Level 7 JHE
Obsidian | Level 7

Perfect, it works.

JHE
Obsidian | Level 7 JHE
Obsidian | Level 7

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

 

 

JHE
Obsidian | Level 7 JHE
Obsidian | Level 7

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;

ballardw
Super User

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.

JHE
Obsidian | Level 7 JHE
Obsidian | Level 7

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.

 

Kurt_Bremser
Super User

@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;
JHE
Obsidian | Level 7 JHE
Obsidian | Level 7

agrere, that is the solution!

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