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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
pangea17
Quartz | Level 8

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

9 REPLIES 9
Reeza
Super User

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. 

 

pangea17
Quartz | Level 8
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.);
Shmuel
Garnet | Level 18

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.

Tom
Super User Tom
Super User

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;

 

 

pangea17
Quartz | Level 8

 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.

Shmuel
Garnet | Level 18

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.

PaigeMiller
Diamond | Level 26

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
pangea17
Quartz | Level 8

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

 

Tom
Super User Tom
Super User

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

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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