BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
radha009
Quartz | Level 8

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
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

2 REPLIES 2
Astounding
PROC Star

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.

ballardw
Super User

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.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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