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

Hi,

 

I have to delete observations with more than 10 variables recorded as 0. I have used macros so far but it is not working and I'm not familiar with array. Each observation has 60 variables. Any suggestions would be very much appreciated. Thanks. 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@catch18 wrote:

Thanks for the links and the suggestion but the code hasn't made a difference so far. I only changed data want, set have.


Below code should work for counting zero's. What @Reeza posted would work for missings.

data have;
  array nvars {20} 8 (20*0);
  do i=1 to 20;
    nvars[i]=i;
    output;
  end;
  stop;
run;

data want;
  set have;
  array myVars _numeric_;
  _n_=0;
  do over myVars;
    if myVars=0 then 
      do;
        _n_+1;
        if _n_>=10 then delete;
      end;
  end;
run;

proc print;
run;

View solution in original post

7 REPLIES 7
catch18
Obsidian | Level 7

I should add that all the variables are numeric. Thanks

Reeza
Super User

If all variables are numeric you can use an array very simply.

 

data want;
set have;

array myVars (*) _numeric_ ; *uses ALL numeric variables;
*array myVars (*) list of variables;

if nmiss(of myvars(*)) >= 10 then delete;

run;

 

Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

 

And here's a tutorial on arrays. 

https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 


@catch18 wrote:

Hi,

 

I have to delete observations with more than 10 variables recorded as 0. I have used macros so far but it is not working and I'm not familiar with array. Each observation has 60 variables. Any suggestions would be very much appreciated. Thanks. 


 

catch18
Obsidian | Level 7

Thanks for the links and the suggestion but the code hasn't made a difference so far. I only changed data want, set have.

Patrick
Opal | Level 21

@catch18 wrote:

Thanks for the links and the suggestion but the code hasn't made a difference so far. I only changed data want, set have.


Below code should work for counting zero's. What @Reeza posted would work for missings.

data have;
  array nvars {20} 8 (20*0);
  do i=1 to 20;
    nvars[i]=i;
    output;
  end;
  stop;
run;

data want;
  set have;
  array myVars _numeric_;
  _n_=0;
  do over myVars;
    if myVars=0 then 
      do;
        _n_+1;
        if _n_>=10 then delete;
      end;
  end;
run;

proc print;
run;
catch18
Obsidian | Level 7

Thanks, but could you please explain this step: array nvars {20} 8 (20*0);?

Patrick
Opal | Level 21

@catch18 wrote:

Thanks, but could you please explain this step: array nvars {20} 8 (20*0);?


This is just creating sample data as you haven't posted any. It creates 20 numerical variables nvars1-nvars20 populated with an initial value of 0.

The array statement is documented here or start with the tutorial @Reeza already shared with you. 

catch18
Obsidian | Level 7

It definitely worked without the first data step! Many thanks!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 900 views
  • 0 likes
  • 3 in conversation