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

Hi there!

I am trying to call a PROC SQL or another based on an input variable.

My input variable is like YYYYMM, and if MM is equal to 01 then I do a new table else I need to merge with the table from the month before.

 

Is there anyway to do this?

I tried but I keep failing it doesn't create a table.

 

Thanks in advance.

 

The code I tried was something like:

 

%let month=%substr(&delta,5,2);
%macro january;
proc sql;
	create table WORK.RH_SOLV_RECEITA_EMITIDA_&delta as 
 	select *
		from WORK.SOLV_RECEITA_EMITIDA_MES;
quit;
%mend janeiro; 
%macro outros;
proc sql;
	create table WORK.RH_SOLV_RECEITA_EMITIDA_&delta as 
	select * FROM WORK.SOLV_RECEITA_EMITIDA_OLD_MES
	outer union corr 
	select * FROM WORK.SOLV_RECEITA_EMITIDA_MES;
quit;
%mend outros;
data _null_;
	if &month = '01' then call execute('%janeiro');
	if &delta <> '01' then call execute('%outros');
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
RicHen
Obsidian | Level 7

So I copied the code @Kurt_Bremser gave me and pasted in the project.

Result, didn't work. 

Copied it into a new project, worked perfectly.

 

Closed and ran the project again and now the code works.

 

Turns out, with a few improvements (like "<>" replaced by "ne"), was working.

It was a bug in the project which got solved by a restart. 😑 🙄

 

Thanks for your time @Kurt_Bremser  and @Tom.

View solution in original post

9 REPLIES 9
Kurt_Bremser
Super User

If macro variable delta contains this:

%let delta=202001;

Then this

if &delta <> '01'

resolves to

if 202001 <> '01'

which will always be false.

The data step will convert your number to a string on its own, but that won't help either.

RicHen
Obsidian | Level 7

delta is in the text format, never the less I changed the data to

%let month=%substr(&delta,5,2);
%macro january;
proc sql;
	create table WORK.RH_SOLV_RECEITA_EMITIDA_&delta as 
 	select *
		from WORK.SOLV_RECEITA_EMITIDA_MES;
quit;
%mend january; 
%macro outros;
proc sql;
	create table WORK.RH_SOLV_RECEITA_EMITIDA_&delta as 
	select * FROM WORK.SOLV_RECEITA_EMITIDA_OLD_MES
	outer union corr 
	select * FROM WORK.SOLV_RECEITA_EMITIDA_MES;
quit;
%mend outros;
data _null_;
	if &month = 1 then call execute('%january');
	if &month <> 1 then call execute('%outros');
run;

and still it doesn't create a table.

 

Kurt_Bremser
Super User

Maxim 2: Read the Log. <> is not interpreted by the data step compiler in the way you think.

Start  simple, and then expand on this code:

%macro january;
%put january executing;
%mend january; 
%macro outros;
%put outros executing;
%mend outros;

%let delta=202001;
%let month=%substr(&delta,5,2);
data _null_;
	if &month = 1 then call execute('%january');
	if &month ne 1 then call execute('%outros'); /* use the proper operator for not equals */
run;

%let delta=202002;
%let month=%substr(&delta,5,2);
data _null_;
	if &month = 1 then call execute('%january');
	if &month ne 1 then call execute('%outros');
run;
RicHen
Obsidian | Level 7

Will do.

Back on the topic when I have more news.

 

Thanks for your support @Kurt_Bremser.

Tom
Super User Tom
Super User

This line has an obviously TRUE test condition.

	if &month <> 1 then call execute('%outros');

It really does not matter what value MONTH has since the MAXimum value of 1 or &MONTH is never going to be zero or missing the test will always be TRUE.

 

Perhaps you are trying to test if &MONTH is NOT EQUAL to 1?  If so then use an appropriate operator for that. 

RicHen
Obsidian | Level 7

So I copied the code @Kurt_Bremser gave me and pasted in the project.

Result, didn't work. 

Copied it into a new project, worked perfectly.

 

Closed and ran the project again and now the code works.

 

Turns out, with a few improvements (like "<>" replaced by "ne"), was working.

It was a bug in the project which got solved by a restart. 😑 🙄

 

Thanks for your time @Kurt_Bremser  and @Tom.

RicHen
Obsidian | Level 7
*Turns out, with a few improvements (like "<>" replaced by "ne"), "the original code" was working.
RicHen
Obsidian | Level 7
Possibly... I had a lot of tryouts before I reached this code.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 1765 views
  • 1 like
  • 3 in conversation