DATA Step, Macro, Functions and more

add new column and disply yes or No based on the data

Accepted Solution Solved
Reply
Contributor
Posts: 67
Accepted Solution

add new column and disply yes or No based on the data

i have data as below

 

Varlist   chg1 chg 2 chg3 chg 4

             yes    No   yes     No

             No     No   No       No

             Yes    No   No       No

 

Expecting as below add new column name patch and if any yes display yes else display no

 

Varlist   chg1 chg 2 chg3 chg 4   Patch

             yes    No   yes     No        yes

             No     No   No       No        No

             Yes    No   No       No        yes

 

my code:

 

data Patch.Unix_final;
set Patch.Unix_final;
    length  patch $4;
array col(*) &varList.;
do i=1 to dim(col);
        if col{i} in ('Yes') then Patch='Yes';

else patch ='No';
end;
run;

 

Thanks a lot.


Accepted Solutions
Solution
‎04-17-2018 12:04 PM
Super User
Posts: 9,914

Re: add new column and disply yes or No based on the data

Just solved one like this a few days ago. Move the = "No" part:

data Patch.Unix_final;
set Patch.Unix_final;
length  patch $4;
array col(*) &varList.;
patch ='No ';
do i=1 to dim(col);
  if col{i} in ('Yes') then Patch='Yes';
end;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎04-17-2018 12:04 PM
Super User
Posts: 9,914

Re: add new column and disply yes or No based on the data

Just solved one like this a few days ago. Move the = "No" part:

data Patch.Unix_final;
set Patch.Unix_final;
length  patch $4;
array col(*) &varList.;
patch ='No ';
do i=1 to dim(col);
  if col{i} in ('Yes') then Patch='Yes';
end;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 23,309

Re: add new column and disply yes or No based on the data

Patch =  ifc(whichc('yes', of chg1-chg4), "yes", "no");

@radha009 wrote:

i have data as below

 

Varlist   chg1 chg 2 chg3 chg 4

             yes    No   yes     No

             No     No   No       No

             Yes    No   No       No

 

Expecting as below add new column name patch and if any yes display yes else display no

 

Varlist   chg1 chg 2 chg3 chg 4   Patch

             yes    No   yes     No        yes

             No     No   No       No        No

             Yes    No   No       No        yes

 

my code:

 

data Patch.Unix_final;
set Patch.Unix_final;
    length  patch $4;
array col(*) &varList.;
do i=1 to dim(col);
        if col{i} in ('Yes') then Patch='Yes';

else patch ='No';
end;
run;

 

Thanks a lot.




PROC Star
Posts: 1,586

Re: add new column and disply yes or No based on the data

data have;
input (chg1 chg2 chg3 chg4) ($);
datalines;
yes    No   yes     No
No     No   No       No
Yes    No   No       No
;

data want;
set have;
array t(*) ch:;
patch=ifc('yes' in t,'yes','No');
run;
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 109 views
  • 3 likes
  • 4 in conversation