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

Hello,

Anyone has an idea how we can assign a value to Macro variable when no rows were selected below? Thanks for any inputs.

proc sql;

select count into : macro_out&j from macro_out;

%put &&macro_out&j;

quit;

Lei

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

Hi,

Sticking with my original suggestion. : )

Add  %let macro_out&j=0;   before your SQL step

When the SQL step runs:

  if there are records in macro_out then count will be >0 and macro_out&j will be assigned the value of count.

  if there are no records in macro_out (because no records that satisfy the having clause), then select count into : macro_out&j will not execute.  And the value of macro_out&j will remain as 0.

As I understand it. : )


The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.

View solution in original post

6 REPLIES 6
Quentin
Super User

Before the proc sql step, add:

%let macro_out&j = /*whatever value you want iuf now rows are selected by SQL step*/  ;

The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
jay_q
Calcite | Level 5

Hi Quentin,

Sorry I wasn't clear about the process. I have a do loop. After each loop, Count(*) from ABC table decreases until no values displayed.  My problem is when no values displayed, Macro variable &&macro_out&j would not get assigned and resolved.  So %if condition would generate error. Thanks.

%do i=1 %to 100;

proc sql;

..............

proc sql;
create table macro_out as
(select exclude_ind,link_id,count(*)as count
from   ABC

where  ind = 'N'
group  by 1,2
having link_id is null);

select count into : macro_out&j from macro_out;
%put &&macro_out&j;
quit;

%let con_exit2=0;

  %if &&macro_out&j = &con_exit2 %then %do;

    %goto exit2;

  %end;

%exit2:

%end;

Quentin
Super User

Hi,

Sticking with my original suggestion. : )

Add  %let macro_out&j=0;   before your SQL step

When the SQL step runs:

  if there are records in macro_out then count will be >0 and macro_out&j will be assigned the value of count.

  if there are no records in macro_out (because no records that satisfy the having clause), then select count into : macro_out&j will not execute.  And the value of macro_out&j will remain as 0.

As I understand it. : )


The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
gamotte
Rhodochrosite | Level 12

As I understand your program, even if the number of rows of ABC has decreased to zero, your table macro_out

should have one row with count=0 and missing values for the other variabes, so your mv macro_out&j. should be valued.

In your example, You are looping on i instead of j. Can this be the explanation ?

You can test the macrovariable &sqlobs. to check if your SQL request returned something.

Peter_C
Rhodochrosite | Level 12

When where returns no rows for a proc sql select statement  the procedure still sets the number of selected rows (0) in the SQLOBS macro var.

so use &sqlobs as that number of rows.

jay_q
Calcite | Level 5

Thank you Quentin!

Tested and sold!

Also, I found using %symexist() also solve my problem:)

/*%if %symexist(macro_out&j)=0 %then %do;*/

/* %goto exit2;*/

/*%end;*/

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 4933 views
  • 2 likes
  • 4 in conversation