If statement 绘图语句中的SAS、if语句

If statement 绘图语句中的SAS、if语句,if-statement,sas,If Statement,Sas,我想将if语句添加到绘图中,以在不同场景中更改标题。 我知道我在第一个sgplot中的代码不起作用。我们有类似的东西吗 %let group = A B C; /*something I want but didn't work*/ proc sgplot data=input_data ; series x=num_accounts y=income; if length(&group.)=2 then title "VAR1=5 and VAR2=4"; else if len

我想将if语句添加到绘图中,以在不同场景中更改标题。 我知道我在第一个sgplot中的代码不起作用。我们有类似的东西吗

%let group = A B C;

/*something I want but didn't work*/
proc sgplot data=input_data ;
series x=num_accounts y=income;
if  length(&group.)=2 then 
title "VAR1=5 and VAR2=4";
else if length(&group.)=1 then
title "VAR1=5";
run;

/*plot without if statement */
proc sgplot data=input_data ;
series x=num_accounts y=income;
title "VAR1=5";
run;     

因为您只是在检查宏变量的长度,是的,这是可能的-在宏
%if
中。但至少必须有一个宏包装器

%let group = A B C;

/*something I want but didn't work*/
proc sgplot data=input_data ;
series x=num_accounts y=income;
if  length(&group.)=2 then 
title "VAR1=5 and VAR2=4";
else if length(&group.)=1 then
title "VAR1=5";
run;

/*plot without if statement */
proc sgplot data=input_data ;
series x=num_accounts y=income;
title "VAR1=5";
run;     
%macro plot_me(group=);

%if %length(&group.)=2 %then %do;
title "VAR1=5 and VAR2=4";
%end;
%else %if %length(&group.)=1 %then %do;
title "VAR1=5";
%end;
proc sgplot data=sashelp.class ;
series x=age y=height;
run;
%mend plot_me;

%let group=A B C;
%plot_me(group=&group.);
ODS Graphics/SGPLOT中的其他选项包括属性映射和注释,它们都是数据步驱动的,因此可能用于更复杂的条件

%let group = A B C;

/*something I want but didn't work*/
proc sgplot data=input_data ;
series x=num_accounts y=income;
if  length(&group.)=2 then 
title "VAR1=5 and VAR2=4";
else if length(&group.)=1 then
title "VAR1=5";
run;

/*plot without if statement */
proc sgplot data=input_data ;
series x=num_accounts y=income;
title "VAR1=5";
run;     
你也可以使用图形模板语言(GTL),如果你要处理更复杂的事情,它会给你更多的选择。这确实允许您编写某种类型的“if”语句

%let group = A B C;

/*something I want but didn't work*/
proc sgplot data=input_data ;
series x=num_accounts y=income;
if  length(&group.)=2 then 
title "VAR1=5 and VAR2=4";
else if length(&group.)=1 then
title "VAR1=5";
run;

/*plot without if statement */
proc sgplot data=input_data ;
series x=num_accounts y=income;
title "VAR1=5";
run;     

宏语言是您介绍的特定示例的正确位置,但GTL或属性映射可能更适合于实际示例,具体取决于您所做的工作。

我运行了您的代码,但没有标题。我在我的真实数据上尝试了它,也没有标题。@user1481397如果没有将SGPLOT设置为使用标题(通常在ODS语句上使用GTITLE选项),标题将不会出现。它也不会出现在你的代码中。不过,这与问题无关。
%let group = A B C;

/*something I want but didn't work*/
proc sgplot data=input_data ;
series x=num_accounts y=income;
if  length(&group.)=2 then 
title "VAR1=5 and VAR2=4";
else if length(&group.)=1 then
title "VAR1=5";
run;

/*plot without if statement */
proc sgplot data=input_data ;
series x=num_accounts y=income;
title "VAR1=5";
run;