DATA Step, Macro, Functions and more

creating dates and inserting them into a macro

Accepted Solution Solved
Reply
Contributor
Posts: 34
Accepted Solution

creating dates and inserting them into a macro

So I was able to create a bunch of dates that I need to use, but I am having a hard time figuring out how to transfer them into a macro.  Using enterprise guide 7.15 and SAS version 9.4.   The following code creates some dates that I need to put into a macro later.

 

data _null_;

call symputx('FDPM', PUT(intnx('MONTH',%SYSFUNC(today()),-1,'b'),month2.)); *First Day of Prior Month;

RUN;

 

&fdpm = 2 for February

 

data dates;

DO Y = 18 to 27; * goes from 19 to 28 last month;

M=intnx('month',today(),-1,'b')+Y;

call symputx(cats('M',Y),put(intnx('month',today(),-1,'b')+Y,date9.));

output;

format m date9.;

END;

RUN;

 

output

Y              M

18     19feb2018

19     20feb2018

20     21feb2018

21     22feb2018

22     23feb2018

23     24feb2018

24     25feb2018

25     26feb2018

26     27feb2018

27     28feb2018

 

%macro download (date, day);

%mend;

%download (cat("&fdpm"d,&Y),"&M1"d);

%download (cat("&fdpm"d,&Y),"&M2"d);

 

This is what I want to final product to look like below:

 

%macro download (date, day);

%mend;

%download (0219, '19FEB2018'd)

%download (0220, '20FEB2018'd)

 

Thought about doing this, but not quite right.

proc sql;

select Y, M

into :Y, :M

from dates;

QUIT;


Accepted Solutions
Solution
‎03-29-2018 11:48 AM
Contributor
Posts: 34

Re: creating dates and inserting them into a macro

Here is what I was looking for.

 

data dates;

DO Y = 18 to 27; * goes from 19 to 28 last month;

M=intnx('month',today(),-1,'b')+Y;

call symputx(cats('M',Y),put(intnx('month',today(),-1,'b')+Y,date9.));

output;

format m date9.;

END;

RUN;

 

%macro download (date, day);

%put &date &day;

%mend;

%macro test;

%do i=1 %to 10;

%download(0&fdpm&&Y&i,"&&M&i"d);

%end;

%mend test;

 

 

%test

 

View solution in original post


All Replies
Super User
Posts: 23,224

Re: creating dates and inserting them into a macro

It's not clear what your question is here. If you're looking for ways to call the macro for the list you created, try CALL EXECUTE.

 

If its how to pass the date to the macro, pass it as is and include the quotes within the macro, make sure they're double quotes. 

 

Contributor
Posts: 34

Re: creating dates and inserting them into a macro

When the macro resolves from this:


%macro download (date, day);
%mend;

%download (cat("&fdpm"d,&Y),"&M1"d);


I want it to see the same as inputting this;

%macro download (date, day);
%mend;

%download (0219, '19FEB2018'd)



The (cat("&fdpm"d,&Y),"& should equal (0219) and the "&M1"d should be read like this ('19FEB2018'd) was typed in.);
Trusted Advisor
Posts: 1,831

Re: creating dates and inserting them into a macro

[ Edited ]

It sems to me that you want to run a macro program - %download - 

for a period inside one month - the previous month of running date, then:

%let from = 19;
%let upto = 28;

data  _NULL_;
      prev_month = today() - day(today();
      MV1 =  put(month(prev_month),z2.);
      MV2 =  substr(put(prev_month,date9.).3,7);
      do dayx = &from to &upto;
           cmd = cat("%download(" , MV1, "," , put(dayx,z2.), MV2 , ")" );
/*DEBUG*/ put cmd=; /* check the log for right syntax*/
/* if OK unmark next line */
*call execute(cmd); end; run;

I have not tested it. In case of please issues post the log.

Super User
Super User
Posts: 7,929

Re: creating dates and inserting them into a macro

You seem have a lot of confusion between what macro code is and what normal SAS code is.

For example this first block of code:

data _null_;
*First Day of Prior Month;
  call symputx('FDPM', PUT(intnx('MONTH',%SYSFUNC(today()),-1,'b'),month2.)); 
RUN;

There is no need use the macro function %SYSFUNC() code to call the normal SAS function TODAY(). You are already running SAS code so you can just call the TODAY() function directly.

data _null_;
*First Day of Prior Month;
  call symputx('FDPM', PUT(intnx('MONTH',today(),-1,'b'),month2.)); 
RUN;

Now this data step

data dates;
  DO Y = 18 to 27; * goes from 19 to 28 last month;
    M=intnx('month',today(),-1,'b')+Y;
    call symputx(cats('M',Y),put(intnx('month',today(),-1,'b')+Y,date9.));
    output;
  END;
  format m date9.;
RUN;

I have no idea what the heck you are trying to do with that code.What is M and what is Y? I looks like you want to have the day of the month go from 18 to 27. WHY?  Why do you want to start from 18?  Why do want to stop at 27?  What is the comment about the 28th have to do with it? Do you want to run from the 18th to the end of the month?

data dates;
  start=intnx('month',today(),-1,'b');
  do date = start+18 to intnx('month',today(),-1,'e'); 
    Y=date-start;
    call symputx(cats('M',y),put(date,date9.));
    output;
  end;
  format start date date9.;
run;

Why are you creating the macro variables?  They are never used for anything.

Now to your macro. Why are you passing the same information into it twice? Couldn't the macro derive one from the other?

%macro download (day);
%local date ;
%let date=%substr(%sysfunc(putn(&day,mmddyy8.)),1,4);
.....
%mend;

Now to call the macro for each record in your data set you can just use call execute.

data _null_;
  set dates;
  call execute(cats('%nrstr(%download)(',date,')'));
run;

Or if you want the log to look prettier and have human readable dates you can generate a date literal to pass into the macro.

data _null_;
  set dates;
  call execute(cats('%nrstr(%download)("',put(date,date9.),'"d)'));
run;

 

 

Contributor
Posts: 34

Re: creating dates and inserting them into a macro

[ Edited ]

 I use the %sysfunc to create global date that I can use elsewhere in the program.  It's a long program, and I am only putting the relevant parts here.

 

The second step I put in what the code generates, they are numbers/dates that are going to be used in a couple of different spots.  You don't need to know why I am generating a certain number of dates. It is running the way I intended.  No, I am not running it to the end of the month. 

 

I have no idea what you are talking about passing information into a macro twice. Again I am saving the variables are being created because I need to use them in the program, I just put the relevant parts of the macro code there to be corrected.

Trusted Advisor
Posts: 1,831

Re: creating dates and inserting them into a macro

Can you focus on your problem ? what have you done and what issue did you have?

No need to deal with "bunch" of macro variables, maybe one or two will be enough.

Respected Advisor
Posts: 2,794

Re: creating dates and inserting them into a macro

I use the %sysfunc to create global date that I can use elsewhere in the program.  It's a long program, and I am only putting the relevant parts here.

 

%sysfunc is not doing what you think it is doing here, as @Tom tried to explain. %sysfunc is entirely unnecessary inside your CALL SYMPUTX, and used the way you are using it, it will not create a global macro variable date that you can use elsewhere in the program. To make the resulting variable FDPM a global macro variable, this can be done in CALL SYMPUTX via setting the  third parameter in CALL SYMPUTX to G (for Global). 

 

http://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=lefunctionsref&docsetTarget=n1...

--
Paige Miller
Solution
‎03-29-2018 11:48 AM
Contributor
Posts: 34

Re: creating dates and inserting them into a macro

Here is what I was looking for.

 

data dates;

DO Y = 18 to 27; * goes from 19 to 28 last month;

M=intnx('month',today(),-1,'b')+Y;

call symputx(cats('M',Y),put(intnx('month',today(),-1,'b')+Y,date9.));

output;

format m date9.;

END;

RUN;

 

%macro download (date, day);

%put &date &day;

%mend;

%macro test;

%do i=1 %to 10;

%download(0&fdpm&&Y&i,"&&M&i"d);

%end;

%mend test;

 

 

%test

 

Super User
Super User
Posts: 7,929

Re: creating dates and inserting them into a macro

[ Edited ]

Glad you are happy with your code.

But your posted code does not work as it is. Perhaps there is something missing?

 

If I change the %DO loop in the %TEST macro it eliminates some of the errors so that at least the second argument to %download() macro looks valid.

612  %macro test;
613  %do i=18 %to 27;
614  %download(0&fdpm&&Y&i,"&&M&i"d);
615  %end;
616  %mend test;
617  %test;
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y18 not resolved.
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y18 not resolved.
0&fdpm&Y18 "19FEB2018"d
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y19 not resolved.
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y19 not resolved.
0&fdpm&Y19 "20FEB2018"d
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y20 not resolved.
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y20 not resolved.
0&fdpm&Y20 "21FEB2018"d
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y21 not resolved.
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y21 not resolved.
0&fdpm&Y21 "22FEB2018"d
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y22 not resolved.
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y22 not resolved.
0&fdpm&Y22 "23FEB2018"d
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y23 not resolved.
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y23 not resolved.
0&fdpm&Y23 "24FEB2018"d
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y24 not resolved.
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y24 not resolved.
0&fdpm&Y24 "25FEB2018"d
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y25 not resolved.
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y25 not resolved.
0&fdpm&Y25 "26FEB2018"d
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y26 not resolved.
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y26 not resolved.
0&fdpm&Y26 "27FEB2018"d
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y27 not resolved.
WARNING: Apparent symbolic reference FDPM not resolved.
WARNING: Apparent symbolic reference Y27 not resolved.
0&fdpm&Y27 "28FEB2018"d

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 143 views
  • 0 likes
  • 5 in conversation