DATA Step, Macro, Functions and more

IF,THEN, ELSE - but ELSE with conditions?

Reply
Contributor
Posts: 39

IF,THEN, ELSE - but ELSE with conditions?

Hello,

 

I'm a little reluctant at using the SAS IF,THEN, ELSE statement when working with multiple subgroups.

For instance, I've got multiple subgroups under QALIST and when I use the if,then,else statement, I want the "ELSE" to be applicable only to a part of the statement (see below). Basically, given QALIST, FINTSNO and BUSSRCCD, we wanna create a new variable (QABSCD). Now I'm afraid to use the ELSE statement (hence why I have been using the "IS NOT") since I don't want every other observation that doesn't respect the conditions of the "IF" to change - is there a way to apply the ELSE statement but only to a predefined subset of variables?

 

Thank you,

 

CF

*If QALIST = 20024, 20070 or 20077 and REFNO2 greater than 0, the QABSCD = 13; 
IF QALIST IN ('20024','20070','20077') AND REFNO2 GT 0 THEN QABSCD = '13';
* If QALIST = 20024 when REFNO2 = 0, code QABSCD = 'UN';
IF QALIST IN ('20024') and REFNO2 EQ 0  THEN QABSCD = "UN";
*For QALIST = 20070 and 20077 when REFNO2 is equal to 0, BUSSRCCD = QABSCD for the BUSSRCD '04','13','14','15','16','99';
IF QALIST IN ('20070','20077') and REFNO2 EQ 0 AND BUSSRCCD IN ('04','13','14','15','16','99') THEN QABSCD = BUSSRCCD;
*For every other BUSSRCCD, QABSCD = 'UN'; 
IF QALIST IN ('20070','20077') and REFNO2 EQ 0 AND BUSSRCCD NOT IN ('04','13','14','15','16','99') THEN QABSCD = 'UN'; 

 

 

*For QALIST = 60001;
* If the FINTSTNO is 35532 and BUSSRCCD is not equal to '04', change the QABSCD to '13';
IF QALIST IN ('60001') AND FINTSTNO IN ('35532') AND BUSSRCCD NOT IN ('04') THEN QABSCD = '13';
* For all other loans, the BUSSRCCD  is equal to QABSCD  ; 
IF QALIST IN ('60001') AND NOT FINTSTNO NOT IN ('35532') and BUSSRCCD IN ('04') THEN QABSCD = BUSSRCCD; 


*For QALIST = 60002;
*If FINTSTNO is '01339','13151','27912','42770','78386','93252','77966' and the BUSSRCCD is '04','13','15','16', BUSSRCCD = QABSCD;
IF QALIST IN ('60002') AND FINTSTNO IN ('01339', '13151', '27912', '42770', '78386','93252','77966') AND BUSSRCCD IN ('04','13','15','16') THEN QABSCD = BUSSRCCD; 
*For all other BUSSRCCD, we change the QABSCD will be equal to '13'; 
IF QALIST IN ('60002') AND FINTSTNO IN ('01339', '13151', '27912', '42770', '78386','93252','77966') AND BUSSRCCD NOT IN ('04','13','15','16') THEN QABSCD = '13'; 
*If the FINTSTNO is not amongst the ones listed above, the QABSCD changes to '14'; 
IF QALIST IN ('60002') AND FINTSTNO NOT IN ('01339', '13151', '27912', '42770', '78386','93252','77966') THEN QABSCD = '14';



*For QALIST = 60004; 
* If the FINTSTNO is '28472',28592','28672','28662','27892','28072', the QABSCD is '13'; 
IF QALIST IN ('60004') AND FINTSTNO IN ('28472','28592','28672','28662','27892','28072') THEN QABSCD = '13';
* If the FINTSTNO is '94057','73902', the QABSCD is '04'; 
IF QALIST IN ('60004') AND FINTSTNO IN ('94057', '73902') THEN QABSCD = '04'; 
*For all other observations within QALIST = 60004, the QABSCD is '14'; 
IF QALIST IN ('60004') AND FINTSTNO NOT IN ('28472','28592','28672','28662','27892','28072','94057','73902') THEN QABSCD = '14'; 


*For QALIST = '60014';
* If the BUSSRCCD is '04' or '13', QABSCD =BUSSRCCD;
IF QALIST IN ('60014') AND BUSSRCD IN ('13','04') THEN QABSCD = BUSSRCCD;
* For all other observations within QALIST = '60014', QABSCD = '14'; 
IF QALIST IN ('60014') AND BUSSRCD NOT IN ('13','04') THEN QABSCD = '14'; 


*For QALIST = '10031' or '30020', the QABSCD = '13'; 
IF QALIST IN ('10031','30020') THEN QABSCD = '13'; 
/*Other lenders*/
IF QALIST IN ('99999') THEN QABSCD = BUSSRCCD; 
RUN; 
SAS Super FREQ
Posts: 3,755

Re: IF,THEN, ELSE - but ELSE with conditions?

Posted in reply to camfarrell25

I don't fully understand your question/logic, but perhaps it would be helpful to know that you can define a block of statements by using the DO-END statements.  The general syntax  is

if condition then do;

   statement1;

   statement2;

   etc;

end;

 

This can help you to logically structure some complex IF-THEN/ELSE logical conditions.  Compare the following two data steps. The first defines a new variables using a series of disjoint IF-THAN/ELSE conditions.  The second is equivalent, but is organized and easier to read. Perhaps the second way is helpful to you?

 

data class1;
set sashelp.class;
length label $15.;
if sex='M' and age<12 then label="Child boy";
if sex='M' and age>=12 then label="Teenage boy";
if sex='F' and age<12 then label="Child girl";
if sex='F' and age>=12 then label="Teenage girl";
run;

data class2;
set sashelp.class;
length label $15.;
if sex='M' then do;
   if age<12 then label="Child boy";
   else label="Teenage boy";
end;
else do;  /* it's a female */
    if age<12 then label="Child girl";
    else label="Teenage girl";
end;
run;

proc compare base=class1 compare=class2;
run;
Super User
Posts: 11,343

Re: IF,THEN, ELSE - but ELSE with conditions?

Posted in reply to camfarrell25

Something else to consider is to use SELECT statements. It will do a comparison, in this case a value of QALIST and only execute those statements related to that value. And then the use of DO/End for sub-qualifications.

 

a brief example:

Select (QALIST);
   when ('20024') do;
                  If RefNo2 gt 0 then  QABSCD = '13';
                  else if RefNo2 eq 0 then  QABSCD = 'UN'; 
                  /* any other code related two '20024'*/
   end;
   when ('20070') do;
                  If RefNo2 gt 0 then  QABSCD = '13';
                  if REFNO2 EQ 0 then do;
                     if BUSSRCCD IN ('04','13','14','15','16','99') THEN QABSCD = BUSSRCCD;
                     else  QABSCD = 'UN'; 
                  end;
   end;
   when ('20077') do;
                  If RefNo2 gt 0 then  QABSCD = '13';
                  if REFNO2 EQ 0 then do;
                     if BUSSRCCD IN ('04','13','14','15','16','99') THEN QABSCD = BUSSRCCD;
                     else  QABSCD = 'UN'; 
                  end;
   end;

   <repeat for other QALIST values>;
   otherwise ; <anything for all other values of QALIST not specified above>
end; /*the QALIST select*/

Select looks at the Variable QALIST and then branches to the code starting with When('value'). Caveat: the VALUE can only appear in ONE when statement. So there is some duplication such as 20070 and 20077. BUT if it turns out that you have to do some thing for just one of those values you add the code within the do/end construct for that single section.

 

Note that you can nest select clauses so if you had to different things for different values of BUSSRCCD within one QALIST value you could have something like (this is just an example for nesting select)

   when ('20070') do;
                  If RefNo2 gt 0 then  QABSCD = '13';
                  if REFNO2 EQ 0 then do;
                     select (BUSSRCCD);
                       when ('04','13','14')  QABSCD = BUSSRCCD;
                       when ('15','16','99')  QABSCD = <something else>;
                       otherwise  QABSCD = 'UN';
                     end;/*bussrccd*/ 
                  end;
   end;

Note that you do not use THEN with WHEN.

 

Ask a Question
Discussion stats
  • 2 replies
  • 326 views
  • 2 likes
  • 3 in conversation