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

Hi Team,

 

I am working on a sas code which I have created a macro variable to invoke with one conditiona and there sometimes the macro variable will be empty and that time it shows error in my log and stoping the job .I need to run the job also if the macro variable is empty and should create the dataset even though if the record is zero. How it is possible to invoke the macro if the macro variable is empty .

 

Thanks

 

Manesh Kp

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoSilva
Quartz | Level 8

Hello,

 

You can do something like this:

 

%macro isBlank(param);
%sysevalf(%superq(param)=,boolean)
%mend isBlank;

%macro runStuff;
%if %isblank(&clm.) = 1 %then %do;
...... NOT Empty code.......
%end;
%else %do;
......Empty code.......
%end;

%mend runStuff;
%runStuff;

Best regards

View solution in original post

10 REPLIES 10
LinusH
Tourmaline | Level 20

Attach your code please (simplified if possible).

Data never sleeps
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Create a template, then insert or append records to it.  Table always exists then:

proc sql;
  create table RESULT (VAR1 char(200),VAR2 num);
  insert into RESULT select * from ADATASET;
/* Or */
insert into RESULT select * from ADATASET where <condition>; quit;
Quentin
Super User

Please show a small example of your problem (code).  Without seeing the macro code and data step code, it's hard to guess.  Please post the error from your log as well.

 

 

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
ambadi007
Quartz | Level 8

I have created one macro variable and trying to extract from one databse giving condition as

where clm_id in ( &clm. ) since the &clm macro var is empty getting a warning message apparent symbolic reference clm  not resolved

BrunoSilva
Quartz | Level 8

Hello,

 

You can do something like this:

 

%macro isBlank(param);
%sysevalf(%superq(param)=,boolean)
%mend isBlank;

%macro runStuff;
%if %isblank(&clm.) = 1 %then %do;
...... NOT Empty code.......
%end;
%else %do;
......Empty code.......
%end;

%mend runStuff;
%runStuff;

Best regards

FreelanceReinh
Jade | Level 19

It should be noted that the WHERE condition

clm_id in ( &clm. )

fails also if macro variable CLM has been defined, but contains a null or blank string.

 

In this case, however, %isblank(&clm.) resolves to 1, so that macro runStuff would incorrectly execute the "NOT Empty code" (presumably including the above WHERE condition).

 

To avoid this, one could initialize macro variable CLM (e.g., with a %LET, %LOCAL or %GLOBAL statement, as appropriate) and then use macro ISBLANK as has been suggested, but execute the "Empty code" if it returns 1 and the "NOT Empty code" if it returns 0. This would also avoid the warning message about an unresolved apparent symbolic reference.

YoungShore
Calcite | Level 5

Hi,

 

I have a related question. I involving the macro as below.

But seems SAS keep checking validation of the null macro variable in STRIP function, ignoring any condition rules like IF...THEN. How can I skip errors like that?

 

data work._tst1;
input sex $ ;
datalines;
0
1
;
run;

 

%let sex = sex ;
%let sexOrienta = ;

 

%macro isBlank(param);
%sysevalf(%superq(param)=,boolean)
%mend isBlank;

 

data work._tst2;
set work._tst1;
has_sexOrienta = %isblank(&sexOrienta); *<--this works fine;
if %isblank(&sexOrienta)=1 then sexOrienta = 'empty'; *<--this works fine if omit the below line;
else sexOrienta=strip(&sexOrienta) ; *<--this line keep causing errors due to strip(null) problem, even condition is not met;
run;

 

Had been searching different key words for solutions, but couldn't find any. Thx a lot.

BrunoSilva
Quartz | Level 8

Hello,

 

I have no SAS to test but I think your problem is that you are mixing datastep if with the macro.

Remember macro will be solved first and datastep if only will use the resolved result of the macro variable.

 

After macro resolve will be something like below:

 

data work._tst2;
    set work._tst1;
    has_sexOrienta = 1; 

    if 1=1 then sexOrienta = 'empty';
    else sexOrienta=strip() ;
run;

and as you see strip() will give the error.

 

Best regards,

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please dont re-open an old post which has been solved.  Post a new question.  Also, don't use macro when it is not needed, there is a base SAS function called missing() which will test this for you:

data want;
  set have;
  sexorienta=ifc(missing(sex),"empty","");
run;

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
  • 10 replies
  • 18477 views
  • 0 likes
  • 7 in conversation