DATA Step, Macro, Functions and more

delete column with condition

Accepted Solution Solved
Reply
Contributor
Posts: 67
Accepted Solution

delete column with condition

[ Edited ]

i have data  with columns Names (win ,unix,sas,sep..etc) and data with yes and no. i want to delete the column without giving the column name.(ex: if sas column has all 'No' data).'

 

Thank you


Accepted Solutions
Solution
‎03-20-2018 10:36 AM
Super Contributor
Posts: 327

Re: delete column with condition

Hello,

 

data have;
    input col1 $ col2 $ col3 $;
cards;
Yes No No
No No Yes
Yes No Yes
;
run;

data NULL_;
    set have end=eof;
    array cols(*) col:;
    array nos(3)_TEMPORARY_;

    do i=1 to dim(cols);
        if strip(cols(i))="No" then nos(i)+1;
    end;

    if eof then do;
        call execute('data want; set have;');
        if max(of nos(*))=_N_ then do;
            call execute('drop');
            do i=1 to dim(cols);
                if nos(i)=_N_ then do;
                    call execute(vname(cols(i)));
                end;
            end;
        end;
call execute('; run;');
    end;
run;

View solution in original post


All Replies
Super User
Posts: 5,849

Re: delete column with condition

This probably need that you first query the table, and based on that issue your drop column statement. Macro logic is what I see.

There have been a lot of similar question on the communities, especially when all column uses are missing. Do a search and youbwould be able to learn from those.
Data never sleeps
Solution
‎03-20-2018 10:36 AM
Super Contributor
Posts: 327

Re: delete column with condition

Hello,

 

data have;
    input col1 $ col2 $ col3 $;
cards;
Yes No No
No No Yes
Yes No Yes
;
run;

data NULL_;
    set have end=eof;
    array cols(*) col:;
    array nos(3)_TEMPORARY_;

    do i=1 to dim(cols);
        if strip(cols(i))="No" then nos(i)+1;
    end;

    if eof then do;
        call execute('data want; set have;');
        if max(of nos(*))=_N_ then do;
            call execute('drop');
            do i=1 to dim(cols);
                if nos(i)=_N_ then do;
                    call execute(vname(cols(i)));
                end;
            end;
        end;
call execute('; run;');
    end;
run;

Contributor
Posts: 67

Re: delete column with condition

Perfect. thanks a lot.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 130 views
  • 0 likes
  • 3 in conversation