DATA Step, Macro, Functions and more

Empty String in IF Statement

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 94
Accepted Solution

Empty String in IF Statement

[ Edited ]

 

The following code works when macro var2 is defined as an non-missing existing variable name. But when I only want var1 and leave var2 empty (basically let the if condition fail and don't do the var2 transformation), it fails. How should I correct it?

 

%let var1 = weight;
%let var2 = age; 
/*%let var2 = ;*/ data want; set have; &var1._1 = &var1 / 2; if "%str(&var2.)" ^= "" then do; &var2._2 = &var2. / 2; end; run;

 


Accepted Solutions
Solution
a week ago
Esteemed Advisor
Posts: 5,529

Re: Empty String in IF Statement

[ Edited ]
Posted in reply to apolitical

I don't think you can generate conditional code without a macro, such as:

 

%macro a(dsn, var1, var2);
data &dsn;
set sashelp.class;
&var1._1 = &var1 / 2;
%if %length(&var2) > 0 %then &var2._2 = &var2. / 2 ;;
run;
%mend;

%a(want1, weight, age);
%a(want2, weight,);

 

PG

View solution in original post


All Replies
Solution
a week ago
Esteemed Advisor
Posts: 5,529

Re: Empty String in IF Statement

[ Edited ]
Posted in reply to apolitical

I don't think you can generate conditional code without a macro, such as:

 

%macro a(dsn, var1, var2);
data &dsn;
set sashelp.class;
&var1._1 = &var1 / 2;
%if %length(&var2) > 0 %then &var2._2 = &var2. / 2 ;;
run;
%mend;

%a(want1, weight, age);
%a(want2, weight,);

 

PG
Super User
Super User
Posts: 8,111

Re: Empty String in IF Statement

[ Edited ]
Posted in reply to apolitical

@apolitical wrote:

 

The following code works when macro var2 is defined as an non-missing existing variable name. But when I only want var1 and leave var2 empty (basically let the if condition fail and don't do the var2 transformation), it fails. How should I correct it?

 

%let var1 = weight;
%let var2 = age; 
/*%let var2 = ;*/ data want; set have; &var1._1 = &var1 / 2; if "%str(&var2.)" ^= "" then do; &var2._2 = &var2. / 2; end; run;

 


Of course it fails.  Just replace the macro variable references with their values and look at the statements that result. 

data want;
set have;
weight_1 = weight / 2;
if "" ^= "" then do;
	_2 =  / 2;
end;
run;

You could get the SAS code to run if you changed it like this

if "%str(&var2.)" ^= "" then do;
	&var2._2 = (&var2.+0) / 2;
end;

Then it would generate valid SAS code.

if "" ^= "" then do;
	_2 =  (+0)/ 2;
end;

Of course I am not sure why you would want to generate the variable _2 , but at least the step will run.

 

Super User
Super User
Posts: 8,111

Re: Empty String in IF Statement

Posted in reply to apolitical

Since you seem to be doing the same thing to each variable why not just use ARRAY processing?

Here is code that takes a list of variables and creates new variables by adding _2 to the variable name and dividing the value of the corresponding old value by 2.

%let varlist = weight age;

data want;
  set sashelp.class;
  array old &varlist ;
  array new %sysfunc(tranwrd(%sysfunc(compbl(&varlist)),%str( ),%str(_2 )))_2;
  do _n_=1 to dim(old);
    new(_n_)=old(_n_)/2 ;
  end;
run;

image.png

Super User
Super User
Posts: 8,111

Re: Empty String in IF Statement

Posted in reply to apolitical

If you are using SAS 9.4M5 then you can use a %IF block in open code.

data want;
  set have;
  &var1._1 = &var1 / 2;
%if %length(&var2) %then %do;
  &var2._2 = &var2. / 2;
%end;
run;
Frequent Contributor
Posts: 94

Re: Empty String in IF Statement

Posted in reply to apolitical

Thanks everyone! 

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 95 views
  • 1 like
  • 3 in conversation