hi code help

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 86
Accepted Solution

hi code help

Hi All,

If i have a 18 some category variables like   Category_1, Category_2, Category_3, Category_4, Category_5, Category_6,..............Category_18 And If i have one more variable like Total_no_Categories...

If any one Category_1 to Category_18 value is 0 then the 'Total_no_Categories' should be -1 of current category value how do we do any sample code please.

Ex:- 1)  Now Total_no_Categories     is   18  (Category_1 to Category_18 has values with out '0')

       2)  If i have a Zero value in Category_8 then the Total_no_Categories value is 17 (18-1)  (Category_8 is zero)

how do we can any one sample code plz

Thanks,

Suresh


Accepted Solutions
Solution
‎08-14-2012 03:43 PM
Super Contributor
Posts: 1,636

Re: hi code help

example:

data have;
input var var1-var5;
cards;
20 1 2 3 0 0
35 2 3 4 0 7
;

data want1;
set have;
array _v(*) var1-var5;
do _n_=1 to dim(_v);
if _v(_n_)=0 then var=var-1;
end;
proc print;run;

/*or */

data want2;
set have;
array _v(*) var1-var5;
do _n_=1 to dim(_v);
if _v(_n_)=0 then do;var=var-1;output;
leave;
end;end;
proc print;run;

Message was edited by: Linlin

View solution in original post


All Replies
Solution
‎08-14-2012 03:43 PM
Super Contributor
Posts: 1,636

Re: hi code help

example:

data have;
input var var1-var5;
cards;
20 1 2 3 0 0
35 2 3 4 0 7
;

data want1;
set have;
array _v(*) var1-var5;
do _n_=1 to dim(_v);
if _v(_n_)=0 then var=var-1;
end;
proc print;run;

/*or */

data want2;
set have;
array _v(*) var1-var5;
do _n_=1 to dim(_v);
if _v(_n_)=0 then do;var=var-1;output;
leave;
end;end;
proc print;run;

Message was edited by: Linlin

Frequent Contributor
Posts: 86

Re: hi code help

Thank you Linlin Smiley Happy

Super Contributor
Posts: 1,636

Re: hi code help

Hi Grandhi4,

Would you please give us credits if the suggested codes helped you?

Thank you!

Frequent Contributor
Posts: 86

Re: hi code help

Sure Linlin. But i don't know how...Credits means 'Start rating' ?  Smiley Sad

Super Contributor
Posts: 1,636

Re: hi code help

You already did that by marking the posts as correct or helpful answers.  Thank you!

Frequent Contributor
Posts: 86

Re: hi code help

Oh ok  Smiley Happy

One more question please...

If i given a Categories =18;     That is hard coded value for variable..... As discussed i have a 18  variables (Category_1, Category_2, Category_3, Category_4,.....Category_18. how can i get a dynamic value for 'Categories' variable ?

Thanks,

Smiley Happy

Respected Advisor
Posts: 3,156

Re: hi code help

If all your variables of interest started by 'category_', then there is a simple way showing below. Otherwise to use proc content or dictionary table/views.

data want;

set have;

array cat category_:;

total=dim(cat);

do over cat;

  total=total-(cat=0);

end;

run;

Haikuo

Respected Advisor
Posts: 3,156

Re: hi code help

Hi,

LinLin has already provided sufficient answer. Here I will just borrow her data and her logic show you some other versions of solution:

data have;

input var1-var5 ;

cards;

1 2 3 0 0

2 3 4 0 7

;

/* data step */

data want;

set have;

array cat var1-var5;

total=dim(cat);

do over cat;

  total=total-(cat=0);

end;

run;

/*Proc SQL*/

proc sql;

select cats("",name ,"<>0") into :lst separated by ','  from dictionary.columns where LIBNAME='WORK' AND MEMNAME='HAVE';

create table want as select *,sum(&lst) as total from have;

quit;

Haikuo

Frequent Contributor
Posts: 86

Re: hi code help

Thank you Haikuo  Smiley Happy

🔒 This topic is solved and locked.

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

Discussion stats
  • 9 replies
  • 466 views
  • 3 likes
  • 3 in conversation