DATA Step, Macro, Functions and more

Macro variable is empty

Accepted Solution Solved
Reply
Contributor
Posts: 68
Accepted Solution

Macro variable is empty

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


Accepted Solutions
Solution
‎05-03-2016 09:19 AM
Contributor
Posts: 51

Re: Macro variable is empty

[ Edited ]
Posted in reply to ambadi007

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


All Replies
Super User
Posts: 5,827

Re: Macro variable is empty

Posted in reply to ambadi007

Attach your code please (simplified if possible).

Data never sleeps
Contributor
Posts: 51

Re: Macro variable is empty

Posted in reply to ambadi007
Super User
Super User
Posts: 9,211

Re: Macro variable is empty

Posted in reply to ambadi007

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;
PROC Star
Posts: 1,433

Re: Macro variable is empty

Posted in reply to ambadi007

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.

 

 

Contributor
Posts: 68

Re: Macro variable is empty

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

Solution
‎05-03-2016 09:19 AM
Contributor
Posts: 51

Re: Macro variable is empty

[ Edited ]
Posted in reply to ambadi007

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

Trusted Advisor
Posts: 1,136

Re: Macro variable is empty

Posted in reply to BrunoSilva

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.

Frequent Learner
Posts: 1

Re: Macro variable is empty

Posted in reply to BrunoSilva

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.

Contributor
Posts: 51

Re: Macro variable is empty

Posted in reply to YoungShore

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,

Super User
Super User
Posts: 9,211

Re: Macro variable is empty

Posted in reply to YoungShore

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;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 2923 views
  • 0 likes
  • 7 in conversation