DATA Step, Macro, Functions and more

do looping issue

Accepted Solution Solved
Reply
Contributor
Posts: 67
Accepted Solution

do looping issue

i am expecting if any yes in a row display yes, if  'No' and blank  in row display No and if all blank display No.

 

data val.compare_results(drop= i);
    set val.compare_results;
    length Impacted_Patch $4;
array val (*) &varlist.;

do i=1 to dim(Val);
        if Val{i} in ('Yes') then Impacted_Patch='Yes';
        else if Val{i} = (' ') then Impacted_Patch=' ';
        else if Val{i} in ('No') and Val{i} in (' ') then Impacted_Patch='No';
    end;
keep ID  Val_required_Nov Val_required_Dec Val_required_Jan Val_required_Feb Val_required_March Val_required_April Impacted_Patch;
run;

 

my results:

 

IDVal_required_NovVal_required_DecVal_required_JanVal_required_FebVal_required_MarchVal_required_AprilImpacted_Patch
5Z4PT7YesYesYesYesYesYesYes
5Z4PTDYes NoYesYesYesYes
5Z4PTENo  YesYesYesYes
5Z4PTPNo NoNoNo  
5Z4PTRYesYesYesYesYesNoYes
5Z4PTXYes YesYesYesYesYes
5Z4PU2Yes YesYesYesDeferredYes
5Z4PU3No NoYesYesYesYes
5Z4PU5YesYesNoYesNoYesYes
5Z4PU8DeferredYesNoYesYesYesYes
5Z4PUA       
5Z4PUMYesYesYesYesYesYesYes
5Z4PUZNo  YesNoNoYes
5Z4PV3       
5Z4PVC       
5Z4PVPNoYesYesYesYes  
5Z4PVXYesYesNoYesNo  
5Z4PVYYesYesNoYesNoYesYes
5Z4PVZ     YesYes

 

expected results:

 

IDVal_required_NovVal_required_DecVal_required_JanVal_required_FebVal_required_MarchVal_required_AprilImpacted_Patchexpected 
5Z4PT7YesYesYesYesYesYesYesyes
5Z4PTDYes NoYesYesYesYesyes
5Z4PTENo  YesYesYesYesyes
5Z4PTPNo NoNoNo  No
5Z4PTRYesYesYesYesYesNoYesyes
5Z4PTXYes YesYesYesYesYesyes
5Z4PU2Yes YesYesYesDeferredYesyes
5Z4PU3No NoYesYesYesYesyes
5Z4PU5YesYesNoYesNoYesYesyes
5Z4PU8DeferredYesNoYesYesYesYesyes
5Z4PUA        
5Z4PUMYesYesYesYesYesYesYesyes
5Z4PUZNo  YesNoNoYesyes
5Z4PV3        
5Z4PVC        
5Z4PVPNoYesYesYesYes  yes
5Z4PVXYesYesNoYesNo  yes
5Z4PVYYesYesNoYesNoYesYesyes
5Z4PVZ     YesYesyes
5Z4PW8No NoNoNo  No
5Z4PWBNoNoDeferredYesNo  yes

Accepted Solutions
Solution
‎05-03-2018 02:45 PM
Super User
Posts: 6,632

Re: do looping issue

[ Edited ]

The main problem you are running into is that you are changing the value of IMPACTED_PATCH each time you inspect a new variable in the array.  While there are many ways to change that, here's one that involves minimal change to your current program.

 

Your current code:

 

do i=1 to dim(Val);
        if Val{i} in ('Yes') then Impacted_Patch='Yes';
        else if Val{i} = (' ') then Impacted_Patch=' ';
        else if Val{i} in ('No') and Val{i} in (' ') then Impacted_Patch='No';
    end;

 

The replacement code:

 

do i=1 to dim(Val) until (Impacted_Patch='Yes');
        if Val{i} in ('Yes') then Impacted_Patch='Yes';
        else if Val{i} in ('No') then Impacted_Patch='No';
    end;

 

It's not clear if you want anything to happen when a variable is "Deferred" so the current logic ignores that.

View solution in original post


All Replies
Solution
‎05-03-2018 02:45 PM
Super User
Posts: 6,632

Re: do looping issue

[ Edited ]

The main problem you are running into is that you are changing the value of IMPACTED_PATCH each time you inspect a new variable in the array.  While there are many ways to change that, here's one that involves minimal change to your current program.

 

Your current code:

 

do i=1 to dim(Val);
        if Val{i} in ('Yes') then Impacted_Patch='Yes';
        else if Val{i} = (' ') then Impacted_Patch=' ';
        else if Val{i} in ('No') and Val{i} in (' ') then Impacted_Patch='No';
    end;

 

The replacement code:

 

do i=1 to dim(Val) until (Impacted_Patch='Yes');
        if Val{i} in ('Yes') then Impacted_Patch='Yes';
        else if Val{i} in ('No') then Impacted_Patch='No';
    end;

 

It's not clear if you want anything to happen when a variable is "Deferred" so the current logic ignores that.

Super User
Posts: 13,321

Re: do looping issue

Since you are looking at "any yes" and "all no"

I might be tempted to do it this way: (Untested as data step to create your example data not provided)

temp= catx(',', of val[*]);
if findw(temp,'Yes')>0 then Impacted_patch='Yes';
else if findw(temp,'No')>0 then Impacted_patch='No'; 

drop temp;

 

Note that the above code will set NO for records involving combinations of Deferred and only No or missing and set missing if the only value is Deferred.

 

You could use the function call that creates temp instead of the variable temp but this way if you can leave temp in while debugging

 

This is also a good example of why coding character Yes No values is poor. If you had them as numeric yes=1 and no=yes

Impacted_patch = ifc(sum(of val[*]),1,0,.);
/* of if you insist Impacted_patch must be character*/
Impacted_patch = ifc(sum(of val[*]),'Yes','No','');

Of course neither of these address "deferred" as you provided no rule for what to do if only that value occurs, or occurs in combination with all other NO or blank.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 96 views
  • 1 like
  • 3 in conversation