BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

I've trouble understanding the macro execution. When I tried to run the code below, .csv file got generated where as it could not be as log.error_table has 0 observations.

 

I'm execpting this code to produce error_table.csv only if log.error_table not equal to zero observations. Could someone guide me to achive this task?

 

%macro export_conditionally;
 

proc sql noprint;
select count(*)  into :count from log.error_table;
quit;


 
%if "&count" ne 0 %then %do;
   proc export data=log.error_table outfile='/usr/sas/tir/work/error_table.csv' dbms=csv replace;
run;

%end;
 
%mend;

%export_conditionally;

 Log:

proc sql noprint;
18        +
  select   
19        + count(*)  into :count from log.error_table;
19        +                                              quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

19        +                                                        proc export data=log.error_table 
outfile='/usr/sas/tir/work/error_table.csv' dbms=csv replace;
19        +
17                                                         The SAS System                         07:03 Saturday, September 26, 2015

  run;     

NOTE: No observations in data set LOG.ERROR_TABLE.
NOTE: Data set has 0 observations.
2 REPLIES 2
Reeza
Super User
1. check what happens to the macro variable if the table does not exist
2. Check your comparison "&count" = 0, I don't think you need the quotes.
Quentin
Super User

Agree with Reeza.  If you turn on options mlogic, you should see that 

%if "&count" ne 0 %then %do;

evalutes to true (not equal) even when &count=0.   The above comparison will compare the three character string "0" to the one character string 0, and return true (not equal).  Because all macro variables resolve to text, you do not need quote marks around text strings.  Suggest you change to:

%if &count ne 0 %then %do;
BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 654 views
  • 0 likes
  • 3 in conversation