Home > Enterprise >  Several if statements
Several if statements

Time:11-25

I want to flag Komp and Bauspar if either one of them is <1 with -, >1 with and if one of them is blank --> no flag. Tried the following, but it produces with two 2022_Bauspar_flag columns somehow? Can you give me hint? Thanks a lot. Kind regards, Ben

%macro target_years2(table,type);
%local name_Bauspar name_Komp;
data &table ;
set work.&table;

         %let name_Komp = "2022_ZZ_Komp"n;
         %let name_Bauspar = "2022_ZZ_Bauspar"n;

         &name_Komp = (1 ("2022_Komposit"n-"2022_Komposit_Ziel"n)/"2022_Komposit_Ziel"n);   
         &name_Bauspar = (1 ("2022_Bausparen"n-"2022_Bausparen_Ziel"n)/"2022_Bausparen_Ziel"n); 

         /*create ZZ_flags*/
         if &name_Komp > 1 THEN do;
         "2022_ZZ_Komp_flag"n = ' '; 
         end;
         else if &name_Komp < 1 and &name_Komp <> .  THEN do;
         "2022_ZZ_Komp_flag"n = '-';
         end;    
         else if &name_Bauspar > 1 THEN do;
         "2022_ZZ_Baupar_flag"n = ' '; 
         end;
         else if &name_Bauspar < 1 and &name_Bauspar <> .  THEN do;
         "2022_ZZ_Bauspar_flag"n = '-';
         end;
         else do;
         end;

run;
%mend;

%target_years2(Produktion_temp,Produktion)

CodePudding user response:

You have a typo in your code. You assign to Baupar_flag in one case, and Bauspar_flag in the other

  else if &name_Bauspar > 1 THEN do;
         "2022_ZZ_Baupar_flag"n = ' '; 
                  ------
         end;
         else if &name_Bauspar < 1 and &name_Bauspar <> .  THEN do;
         "2022_ZZ_Bauspar_flag"n = '-';
                  -------

CodePudding user response:

Difficult to help you as you do not provide any output or detailed explanation of what is wrong.

Note that if you want to compute both columns for each observations you would need to split your if statement. The second IF condition is not evaluated when the first IF condition is true.

I understand you want to compute two derived columns 2022_ZZ_Komp_flag and 2022_ZZ_Bauspar_flag with the following condition:

  • if associated macro variable &name_ > 1 then flag is
  • if associated macro variable &name_ < 1 then flag is -
  • if associated macro variable &name_ = . then flag is missing

With the following dataset

data have;
input zz_komp zz_baupar;
cards;
0.9 1.1
1.1 0.8
. 2
0.8 .
;

The following code

data want;
set have;

"2022_ZZ_Komp_flag"n = ifc(zz_komp > 1, ' ', '-');
"2022_ZZ_Baupar_flag"n = ifc(zz_baupar > 1, ' ', '-');

if missing(zz_komp) then "2022_ZZ_Komp_flag"n = '';
if missing(zz_baupar) then "2022_ZZ_Baupar_flag"n = '';
run;

Produces

enter image description here

Is it the expected result?

  • Related