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

Hello All,

 

I have the below program:

 

 

%let myDT = '23JAN2022'D;

%macro dts(mnth_1=);
	data want;
		test_dt=&mnth_1.;
		format test_dt date9.;
	run;

	%put @@@&myDT;
	%put @@@&mnth_1;
%mend;

%dts(mnth_1=intnx('month', &myDT., -1, 's'));

 

 

Initially, I have the macro variable

&myDT.

which I pass it to the macro

%dts(mnth_1=intnx('month', &myDT., -1, 's'))

in order to find last month's date.

 

 

Even though I am able to use the

&mnth_1.

in order to create the dataset, when I use the

%PUT

statement to write the value it doesn't work.

 

Any ideas please?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

To print the value of a VARIABLE use the PUT statement.  The %PUT statement is a MACRO STATEMENT and so will know nothing about any variables you might have created in your data step.

 

%macro dts(mnth_1=);

%put &=mnth_1;
data want;
  test_dt=&mnth_1.;
  format test_dt date9.;
  put test_dt = ;
run;

%mend;

Let's try it:

2106  %let myDT = '23JAN2022'D;
2107  %dts(mnth_1=intnx('month', &myDT., -1, 's'));
MNTH_1=intnx('month', '23JAN2022'D, -1, 's')
MPRINT(DTS):   data want;
MPRINT(DTS):   test_dt=intnx('month', '23JAN2022'D, -1, 's');
MPRINT(DTS):   format test_dt date9.;
MPRINT(DTS):   put test_dt = ;
MPRINT(DTS):   run;

test_dt=23DEC2021
NOTE: The data set WORK.WANT has 1 observations and 1 variables.

View solution in original post

7 REPLIES 7
Zatere
Quartz | Level 8
The macro mnth_1=intnx('month', &myDT., -1, 's') is used to create a variable in an existing dataset.
As you might have seen, this part of the program works as expected.
However, I would like to write the &mnth_1.
Is that possible?
Thanks.
PaigeMiller
Diamond | Level 26

I'm going to avoid the macro %dts and just create a macro variable named &dts.

 

%let dts=%nrstr(mnth_1=intnx('month', &myDT., -1, 's'));
%put &=dts;

Then you can use &DTS anywhere you want. As to why you want this, I still don't understand, and I get the feeling you have over-complicated things. 

 

You could also do this to create macro variable &MNTH_1

 

%let mnth1 = %sysfunc(intnx(month,&mydt,-1,s));

So, please explain in much more detail what you want, why you want it, and even better ... give us an example of what you are doing without macros and without macro variables. As I said, I think you have greatly over-complicated things.

--
Paige Miller
Tom
Super User Tom
Super User

To print the value of a VARIABLE use the PUT statement.  The %PUT statement is a MACRO STATEMENT and so will know nothing about any variables you might have created in your data step.

 

%macro dts(mnth_1=);

%put &=mnth_1;
data want;
  test_dt=&mnth_1.;
  format test_dt date9.;
  put test_dt = ;
run;

%mend;

Let's try it:

2106  %let myDT = '23JAN2022'D;
2107  %dts(mnth_1=intnx('month', &myDT., -1, 's'));
MNTH_1=intnx('month', '23JAN2022'D, -1, 's')
MPRINT(DTS):   data want;
MPRINT(DTS):   test_dt=intnx('month', '23JAN2022'D, -1, 's');
MPRINT(DTS):   format test_dt date9.;
MPRINT(DTS):   put test_dt = ;
MPRINT(DTS):   run;

test_dt=23DEC2021
NOTE: The data set WORK.WANT has 1 observations and 1 variables.
Zatere
Quartz | Level 8
Thank you very much.
I was hoping that I can use the %PUT statement.
However, your solution does what I wanted, meaning to print the value.
andreas_lds
Jade | Level 19

I don't see the need to use a macro at all. Why can't you use

mnth_1 = intnx('month', &myDT., -1, 's');

in a data step?

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 7 replies
  • 1062 views
  • 0 likes
  • 5 in conversation