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;
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,);
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,);
@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.
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;
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;
Thanks everyone!
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.
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.