The script below is not giving the expected result.
Please correct the script
/*%let cie=b2;*/
%let cie=b2;
data class;
format company $1.;
set sashelp.class;
if &cie eq b1 then company="A";
else if &cie eq b2 then company ="B";
run;
In the case of my code where I treat the macro variable as text and compare it to text or @Quentin code that uses a macro %IF condition, you will get a value of Company=B for every observation in the Class data set. The following code works for me:
%let cie=b2;
data class;
format company $1.;
set sashelp.class;
if "&cie" eq "b1" then company="A";
else if "&cie" eq "b2" then company ="B";
run;
proc print;
run;
Output:
Obs company Name Sex Age Height Weight
1 B Alfred M 14 69.0 112.5
2 B Alice F 13 56.5 84.0
3 B Barbara F 13 65.3 98.0
4 B Carol F 14 62.8 102.5
5 B Henry M 14 63.5 102.5
6 B James M 12 57.3 83.0
7 B Jane F 12 59.8 84.5
8 B Janet F 15 62.5 112.5
9 B Jeffrey M 13 62.5 84.0
10 B John M 12 59.0 99.5
11 B Joyce F 11 51.3 50.5
12 B Judy F 14 64.3 90.0
13 B Louise F 12 56.3 77.0
14 B Mary F 15 66.5 112.0
15 B Philip M 16 72.0 150.0
16 B Robert M 12 64.8 128.0
17 B Ronald M 15 67.0 133.0
18 B Thomas M 11 57.5 85.0
19 B William M 15 66.5 112.0
If this is not working for you, please send your log.
There are no variables named b1 or b2 in the sashelp.class data set. What are you expecting this step to do?
If you are expecting this to be treated as a string, then try:
%let cie=b2;
data class;
format company $1.;
set sashelp.class;
if "&cie" eq "b1" then company="A";
else if "&cie" eq "b2" then company ="B";
run;
@alepage wrote:
this script is not working.
Please explain further, as I said earlier in this thread, very brief explanations are rarely sufficient.
Explain what you want your code to do.
I want to map company ='A' if the macro variable &cie = b1
and map company ='B' if the macro variable &cie=b2
@alepage wrote:
I want to map company ='A' if the macro variable &cie = b1
and map company ='B' if the macro variable &cie=b2
Of course even with this brief description, we don't know if b1 is a variable in your data set or a value of the variable. Similarly for b2. And we don't know if the value of &cie is supposed to be a text string or the name of a variable or something else. These very brief problem descriptions are rarely sufficient.
Please explain in words the problem you are trying to solve using the macro variable, making it clear what &cie contains and what b1 and b2 represent.
Also, you didn't provide a data set (which you should always do), so that would make things clearer. Use these instructions and examples to provide a data set.
One of the key parts to learning the macro language is to learn the difference between %IF and IF.
If you want to use the macro language to decide which SAS code the macro will generate, you use the %IF statement.
It sounds like you might want:
%let cie=b2;
data class;
format company $1.;
set sashelp.class;
%if &cie=b1 %then %do;
company="A";
%end;
%else %if &cie=b2 %then %do;
company="B";
%end;
run;
You seem unwilling to explain in detail what you are trying to do. This would help greatly and probably you would already have an answer if we had a better explanation.
The specific error message is that you cannot use nested %IFs outside of a macro in SAS, and is an entirely different problem than the one you started the thread with. But since you haven't shown us your code and haven't explained the points that several people have mentioned, its nearly impossible to help.
Please don't be so reluctant to provide the information and detail we have asked for. Please be generous with information.
Sorry, my macro code will only work if the code is inside a macro definition.
@Quentin wrote:
Sorry, my macro code will only work if the code is inside a macro definition.
It will also work if the "nesting" is avoided by omitting the %else keyword.
@alepage wrote:
ERROR: Nesting of %IF statements in open code is not supported. %IF ignored.
ERROR: Skipping to next %END statement.
You can either remove the %ELSE and make each %IF independent.
%if &cie=b1 %then %do;
company="A";
%end;
%if &cie=b2 %then %do;
company="B";
%end;
Or perhaps you don't need the second %IF at all and can instead just use the %ELSE.
%if &cie=b1 %then %do;
company="A";
%end;
%else %do;
company="B";
%end;
Alternatively, you can nest %SYSFUNC function calls instead of %IF statements:
company="%sysfunc(choosec(%sysfunc(whichc(&cie,b1,b2)),A,B))";
This code can be generalized easily to handle more than two value pairs. You can also specify a value (for variable COMPANY) indicating that macro variable CIE contains an unexpected value:
company="%sysfunc(choosec(%sysfunc(whichc(&cie,b1,b2))+1,?,A,B))";
(Remember that WHICHC returns zero in that case, so the +1 shifts the first argument of the CHOOSEC function appropriately.)
Or the same using DATA step logic:
company=choosec(whichc("&cie","b1","b2")+1,"?","A","B");
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.