DATA Step, Macro, Functions and more

SYSFUNC

Reply
Occasional Contributor
Posts: 10

SYSFUNC

Hi Folks,

 

Can anyone say what will be  the output for the below code? 

 

 

%global PR_FA_H
PR_F_A
PR_F_I
PR_F_S
PR_T_C
PR_TA_C 
PR_T_V 
PR_T_O 
PR_T_OT 
PR_TA
ENTI
DEFINE_ENT
PR_SA;

%macro autofecha;
%let par1=%sysfunc(putn(%sysevalf("&sysdate"d-0),weekday.));
%put par1 = &par1;
%if &par1 <= 6 %then %let par2= %sysfunc(putn(%sysevalf("&sysdate"d-(&par1 + 1)),date9.));
%else %let par2= %sysfunc(putn(%sysevalf("&sysdate"d-(1)),date9.));
%put par2=&par2;
%let PR_FA_H=%sysfunc(putn(%sysfunc(intnx(month,"%sysfunc(putn("&par2"d,date9.))"d,-0, begin)),date9.));
%let PR_F_A=%sysfunc(putn(%sysfunc(intnx(month,"%sysfunc(putn("&par2"d,date9.))"d,-13, begin)),date9.));
%let PR_F_I=%sysfunc(putn(%sysfunc(intnx(month,"%sysfunc(putn("&par2"d,date9.))"d,-49, begin)),date9.));
%let PR_F_S=%sysfunc(putn(%sysfunc(intnx(month,"%sysfunc(putn("&par2"d,date9.))"d,-7, begin)),date9.));
%put PR_FA_H=&PR_FA_H;
%put PR_F_A=&PR_F_A;
%put PR_F_I=&PR_F_I;
%put PR_F_S=&PR_F_S;
%let PR_T_C = 1.2;
%let PR_TA_C = 2.0;
%let PR_T_V = 1.1;
%let PR_T_O = 2.0;
%let PR_T_OT = 2.0;
%let PR_TA = 2.0;
%let ENTI=9999999999; 
%let DEFINE_ENT= le; 
%let PR_SA=MCDO; 
%mend autofecha;
%autofecha;

Super User
Super User
Posts: 9,208

Re: SYSFUNC

Depending on whether the code actually works you may have some global macro variables, and there may be some printed output in the log as to what those macro variables contain.  Me, my delete button finger is just itching looking at that code.

Super User
Posts: 9,568

Re: SYSFUNC

[ Edited ]

A lot of PITA.

 

I'd never write such an ugly abomination. My fingers would resist to the point of breaking themselves.

 

If you need those macro variables for use later in programs, define them in a data step (at least those calculated from dates).

 

No macro defintion is then needed (the if-then-else happens in the data step).


@c4shiva wrote:

Hi Folks,

 

Can anyone say what will be  the output for the below code? 

 

 

%global PR_FA_H
PR_F_A
PR_F_I
PR_F_S
PR_T_C
PR_TA_C 
PR_T_V 
PR_T_O 
PR_T_OT 
PR_TA
ENTI
DEFINE_ENT
PR_SA;

%macro autofecha;
%let par1=%sysfunc(putn(%sysevalf("&sysdate"d-0),weekday.));
%put par1 = &par1;
%if &par1 <= 6 %then %let par2= %sysfunc(putn(%sysevalf("&sysdate"d-(&par1 + 1)),date9.));
%else %let par2= %sysfunc(putn(%sysevalf("&sysdate"d-(1)),date9.));
%put par2=&par2;
%let PR_FA_H=%sysfunc(putn(%sysfunc(intnx(month,"%sysfunc(putn("&par2"d,date9.))"d,-0, begin)),date9.));
%let PR_F_A=%sysfunc(putn(%sysfunc(intnx(month,"%sysfunc(putn("&par2"d,date9.))"d,-13, begin)),date9.));
%let PR_F_I=%sysfunc(putn(%sysfunc(intnx(month,"%sysfunc(putn("&par2"d,date9.))"d,-49, begin)),date9.));
%let PR_F_S=%sysfunc(putn(%sysfunc(intnx(month,"%sysfunc(putn("&par2"d,date9.))"d,-7, begin)),date9.));
%put PR_FA_H=&PR_FA_H;
%put PR_F_A=&PR_F_A;
%put PR_F_I=&PR_F_I;
%put PR_F_S=&PR_F_S;
%let PR_T_C = 1.2;
%let PR_TA_C = 2.0;
%let PR_T_V = 1.1;
%let PR_T_O = 2.0;
%let PR_T_OT = 2.0;
%let PR_TA = 2.0;
%let ENTI=9999999999; 
%let DEFINE_ENT= le; 
%let PR_SA=MCDO; 
%mend autofecha;
%autofecha;


 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,537

Re: SYSFUNC

Yes, the code is ugly.  Yes, you get a bunch of macro variables.  But it is possible to tell you a bit more about the result.

 

For some of the macro variables, what you see is what you get.

 

For others, the result depends on the day that the code runs, as well as the day of the week that it runs.

 

You can see the value of all macro variables created here by adding one line at the end of the program:

 

%put _user_;

 

Run it and see what you get.

Occasional Contributor
Posts: 10

Re: SYSFUNC

Posted in reply to Astounding

Actually am not having SAS to execute the code. My work is to convert sas code to a tech spec document.So only not able to get the output. Smiley Sad

Super User
Posts: 9,568

Re: SYSFUNC


@c4shiva wrote:

Actually am not having SAS to execute the code. My work is to convert sas code to a tech spec document.So only not able to get the output. Smiley Sad


Demanding that from you is like demanding to put in nails without a hammer. Somebody needs to turn on their thinking equipment.

 

Now, here's code I made up from the mess you got:

data _null_;
par1 = weekday(today());
if par1 le 6 /* Sunday to Friday */
then par2 = today() - par1 + 1; /* Make Monday's date */
else par2 = today() - 1; /* if Saturday, make Friday's date */
PR_FA_H = intnx('month',par2,0,'b'); /* begin of month of par2 */
PR_F_A = intnx('month',par2,-13,'b'); /* 1 year + 1 month past */
PR_F_I = intnx('month',par2,-49,'b'); /* 4 years + 1 month past */
PR_F_S = intnx('month',par2,-7,'b'); /* 7 months past */
call symputx('PR_FA_H',put(PR_FA_H,date9.),'g');
call symputx('PR_F_A',put(PR_F_A,date9.),'g');
call symputx('PR_F_I',put(PR_F_I,date9.),'g');
call symputx('PR_F_S',put(PR_F_S,date9.),'g');
call symputx('PR_T_C','1.2','g');
call symputx('PR_TA_C','2.0','g');
call symputx('PR_T_V','1.1','g');
call symputx('PR_T_O','2.0','g');
call symputx('PR_T_OT','2.0','g');
call symputx('PR_TA','2.0','g');
call symputx('ENTI','9999999999','g'); 
call symputx('DEFINE_ENT','le','g'); 
call symputx('PR_SA','MCDO','g'); 
run;

%put _user_;

Note:

  • it's considerably shorter and easier to read than that abominable macro concoction
  • it's got comments!

I've tested it to make sure it delivers the same results, and the comments should make it clear what is calculated.

Here's the log from running that code:

28         data _null_;
29         par1 = weekday(today());
30         if par1 le 6 /* Sunday to Friday */
31         then par2 = today() - par1 + 1; /* Make Monday's date */
32         else par2 = today() - 1; /* if Saturday, make Friday's date */
33         PR_FA_H = intnx('month',par2,0,'b'); /* begin of month of par2 */
34         PR_F_A = intnx('month',par2,-13,'b'); /* 1 year + 1 month past */
35         PR_F_I = intnx('month',par2,-49,'b'); /* 4 years + 1 month past */
36         PR_F_S = intnx('month',par2,-7,'b'); /* 7 months past */
37         call symputx('PR_FA_H',put(PR_FA_H,date9.),'g');
38         call symputx('PR_F_A',put(PR_F_A,date9.),'g');
39         call symputx('PR_F_I',put(PR_F_I,date9.),'g');
40         call symputx('PR_F_S',put(PR_F_S,date9.),'g');
41         call symputx('PR_T_C','1.2','g');
42         call symputx('PR_TA_C','2.0','g');
43         call symputx('PR_T_V','1.1','g');
44         call symputx('PR_T_O','2.0','g');
45         call symputx('PR_T_OT','2.0','g');
46         call symputx('PR_TA','2.0','g');
47         call symputx('ENTI','9999999999','g');
48         call symputx('DEFINE_ENT','le','g');
49         call symputx('PR_SA','MCDO','g');
50         run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

51         
52         %put _user_;
GLOBAL DEFINE_ENT le
GLOBAL ENTI 9999999999
GLOBAL PR_FA_H 01APR2018
GLOBAL PR_F_A 01MAR2017
GLOBAL PR_F_I 01MAR2014
GLOBAL PR_F_S 01SEP2017
GLOBAL PR_SA MCDO
GLOBAL PR_TA 2.0
GLOBAL PR_TA_C 2.0
GLOBAL PR_T_C 1.2
GLOBAL PR_T_O 2.0
GLOBAL PR_T_OT 2.0
GLOBAL PR_T_V 1.1

(I've snipped away all the other _user_ macrovars that are created by Enterprise Guide and the SAS system automatically)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 10

Re: SYSFUNC

Posted in reply to KurtBremser

Thanks a lot KurtBremser

Ask a Question
Discussion stats
  • 6 replies
  • 136 views
  • 1 like
  • 4 in conversation