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 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;
I should add that all the variables are numeric. Thanks
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.
Thanks for the links and the suggestion but the code hasn't made a difference so far. I only changed data want, set have.
@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;
Thanks, but could you please explain this step: array nvars {20} 8 (20*0);?
@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.
It definitely worked without the first data step! Many thanks!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.