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

Hi All,

 

Please help me...

 

I am looking for a solution to a very basic problem. I am new to SAS and don't use it too often, so get quite tied up in the formatting.

 

I have a check I'd like to do whereby I check a prompt date (Cut_off_date_prompt is currently same as Cutoff_Date) against another date and abort/cancel if the two are different. Forgive me if my code is terrible.

 

Program code is below, excerpt of the log below that. My prompt works fine, as does the conversion. FM variable is returning NOV60 not MAR21. I am assuming this is because it is reading this as a number, but am unsure how to convert that to a date?

 

PROGRAM

%let Cutoff_Date = '01Mar2021'd ;
%put &Cutoff_Date;

%let fileyear = 2021;
%let filemonth = 0321;
%let prevfileyear = 2021;
%let prevfilemonth = 0221;

 

%let CoDP = %sysfunc(inputn(&cutoff_date_prompt,date9.),monyy.);
%let FM = %sysfunc(inputn(&filemonth,MMYYn4.),monyy.);

%put &CoDP;
%put &FM;

%macro datecheck;
%if &CoDP ne &FM %then
%abort cancel;
%mend;

 

 

LOG

 

20 NOGTITLE
21 NOGFOOTNOTE
22 GPATH=&sasworklocation
23 ENCODING=UTF8
24 options(rolap="on")
25 ;
26
27 GOPTIONS ACCESSIBLE;
28
29 %let Cutoff_Date = '01Mar2021'd ;
30 %put &Cutoff_Date;
'01Mar2021'd
31
32 %let fileyear = 2021;
33 %let filemonth = 0321;
34 %let prevfileyear = 2021;
35 %let prevfilemonth = 0221;
36
37 %let CoDP = %sysfunc(inputn(&cutoff_date_prompt,date9.),monyy.);
38
39
40 %let FM = %sysfunc(inputn(&filemonth,MMYYn4.),monyy.);
41
42 %put &CoDP;
MAR21
43 %put &FM;
NOV60
44
45 %macro datecheck;
46 %if &CoDP ne &FM %then
2 The SAS System 12:21 Thursday, March 4, 2021

47 %abort cancel;
48 %mend;
49
50 %put _user_;

 

 

Please help!

 

Thanks.

Greedy

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

First, please help us out, from now on (as in 100% of the time in the future), please paste your log into the window that appears when you click on the </> icon. This causes the log to be properly formatted and much more readable.

 

When using macro variables, you are best off not formatting them (except in the case where you need to put them into a title or label or some other form which must be human readable). Then you don't have to go through the trouble of unformatting and/or formatting them.

 

So 

 

%let Cutoff_Date = %sysevalf('01Mar2021'd) ; /* Note: not formatted */
%put &=Cutoff_Date;

%let fileyear = 2021;
%let filemonth = 0321;
%let prevfileyear = 2021;
%let prevfilemonth = 0221;

%let CoDP = %sysfunc(putn(&cutoff_date,monyy.));
%let FM = %sysfunc(inputn(01&filemonth,ddmmyy.),monyy.);


%put &=CoDP;
%put &=FM;

but since &CoDP and &FM don't need to be formatted to test their equality, I suggest using built-in SAS function INTNX that can determine the first day of each month, and then you can compare those to each other, without formatting. Note: I have forced FM to be the first day of the month. So now your code doesn't need formatted dates that are human readable to work.

 

%let CoDP = %sysfunc(intnx(month,&cutoff_date,0,b)); /* Note: Not formatted */
%let FM = %sysfunc(inputn(01&filemonth,ddmmyy.)); /* Note: Not formatted */

%put &=CoDP;
%put &=FM;

Lastly, you tried to you an informat MMYYN4., which doesn't exist.

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

First, please help us out, from now on (as in 100% of the time in the future), please paste your log into the window that appears when you click on the </> icon. This causes the log to be properly formatted and much more readable.

 

When using macro variables, you are best off not formatting them (except in the case where you need to put them into a title or label or some other form which must be human readable). Then you don't have to go through the trouble of unformatting and/or formatting them.

 

So 

 

%let Cutoff_Date = %sysevalf('01Mar2021'd) ; /* Note: not formatted */
%put &=Cutoff_Date;

%let fileyear = 2021;
%let filemonth = 0321;
%let prevfileyear = 2021;
%let prevfilemonth = 0221;

%let CoDP = %sysfunc(putn(&cutoff_date,monyy.));
%let FM = %sysfunc(inputn(01&filemonth,ddmmyy.),monyy.);


%put &=CoDP;
%put &=FM;

but since &CoDP and &FM don't need to be formatted to test their equality, I suggest using built-in SAS function INTNX that can determine the first day of each month, and then you can compare those to each other, without formatting. Note: I have forced FM to be the first day of the month. So now your code doesn't need formatted dates that are human readable to work.

 

%let CoDP = %sysfunc(intnx(month,&cutoff_date,0,b)); /* Note: Not formatted */
%let FM = %sysfunc(inputn(01&filemonth,ddmmyy.)); /* Note: Not formatted */

%put &=CoDP;
%put &=FM;

Lastly, you tried to you an informat MMYYN4., which doesn't exist.

--
Paige Miller
Rick_SAS
SAS Super FREQ

Just a trivial comment: You will rarely need to use a %sysevalf around a SAS time or date or datetime constant. The parser converts strings like '01Mar2021'd to a number, so you can use it directly in the subsequent function calls.

 

%let Cutoff_Date = '01Mar2021'd; 

ballardw
Super User

I'm pretty sure that you are getting NOV60 because your install is using a "best" informat when MMYYN4. doesn't exist. At least I get errors and can't find a reference to an MMYYN Informat.

 

Consider:

124  data junk;
125     x= 0321;
126     put x= date9. x=monyy5.;
127  run;

x=17NOV1960 x=NOV60

So I think you are getting the input resulting in the numeric 312 and not a "date" in 2021.

 

Don't know why your log doesn't show the issues with the informat attempt that I do:

100   %let FM = %sysfunc(inputn(&filemonth,MMYYn4.));
WARNING: Argument 2 to function INPUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is
         out of range.
NOTE: Mathematical operations could not be performed during %SYSFUNC function execution. The
      result of the operations have been set to a missing value.

Since arguement 2 is the informat name...

 

Do you have a custom informat somewhere name MMYYN?

Reeza
Super User
%let Cutoff_Date = '01Mar2021'd ;
%let prev_month = %sysfunc(intnx(month, &cutoff_date, -1, s));


%let fileyear = %sysfunc(putn(&cutoff_date, year4.));
%let filemonth = %sysfunc(putn(&cutoff_date, mmyyn4.));
%let prevfileyear = %sysfunc(putn(&prev_month, year4.));
%let prevfilemonth =  %sysfunc(putn(&prev_month, mmyyn4.));
%let FM =  %sysfunc(putn(&cutoff_date,  monyy5.));
 
 
 
 %put &Cutoff_Date;
 %put &Prev_month.;
 %PUT &fileYear.;
 %put &fileMonth.;
 %put &prevfileyear;
 %put &prevfileMonth.;
 %put &fm.;

 

Make sure to use date logic and functions. See above for an example that matches what you're trying to do but uses two dates as the starting point and then formats to customize them as needed for presentation. Note that you can create multiple variables or you can modify your code to have the %SYSFUNC() where it needs it as well. 

 

Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 


@GreedySheedy wrote:

Hi All,

 

Please help me...

 

I am looking for a solution to a very basic problem. I am new to SAS and don't use it too often, so get quite tied up in the formatting.

 

I have a check I'd like to do whereby I check a prompt date (Cut_off_date_prompt is currently same as Cutoff_Date) against another date and abort/cancel if the two are different. Forgive me if my code is terrible.

 

Program code is below, excerpt of the log below that. My prompt works fine, as does the conversion. FM variable is returning NOV60 not MAR21. I am assuming this is because it is reading this as a number, but am unsure how to convert that to a date?

 

PROGRAM

%let Cutoff_Date = '01Mar2021'd ;
%put &Cutoff_Date;

%let fileyear = 2021;
%let filemonth = 0321;
%let prevfileyear = 2021;
%let prevfilemonth = 0221;

 

%let CoDP = %sysfunc(inputn(&cutoff_date_prompt,date9.),monyy.);
%let FM = %sysfunc(inputn(&filemonth,MMYYn4.),monyy.);

%put &CoDP;
%put &FM;

%macro datecheck;
%if &CoDP ne &FM %then
%abort cancel;
%mend;

 

 

LOG

 

20 NOGTITLE
21 NOGFOOTNOTE
22 GPATH=&sasworklocation
23 ENCODING=UTF8
24 options(rolap="on")
25 ;
26
27 GOPTIONS ACCESSIBLE;
28
29 %let Cutoff_Date = '01Mar2021'd ;
30 %put &Cutoff_Date;
'01Mar2021'd
31
32 %let fileyear = 2021;
33 %let filemonth = 0321;
34 %let prevfileyear = 2021;
35 %let prevfilemonth = 0221;
36
37 %let CoDP = %sysfunc(inputn(&cutoff_date_prompt,date9.),monyy.);
38
39
40 %let FM = %sysfunc(inputn(&filemonth,MMYYn4.),monyy.);
41
42 %put &CoDP;
MAR21
43 %put &FM;
NOV60
44
45 %macro datecheck;
46 %if &CoDP ne &FM %then
2 The SAS System 12:21 Thursday, March 4, 2021

47 %abort cancel;
48 %mend;
49
50 %put _user_;

 

 

Please help!

 

Thanks.

Greedy


 

GreedySheedy
Calcite | Level 5

Thank you for the replies and feedback/tips, it's much appreciated.

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1558 views
  • 7 likes
  • 5 in conversation