Help using Base SAS procedures

Array

Reply
Contributor
Posts: 66

Array

i wnat the count when 0 has occured in these month from jan to jun

data l;
input jan feb mar apr may jun;
cards;
1 2 3 4 5 0
9 8 7 0 6 5
4 5 2 5 0 9
9 0 9 8 7 6
run;

output
jan feb mar apr may jun tot
1 2 3 4 5 0 1
9 8 7 0 6 5 3
4 5 2 5 0 9 2
9 0 9 8 7 6 5

bcuse the 0 has occured in the dofferenct positions in first tot is 1 bcse it has occured in the first postion like that i want.
Occasional Contributor P_J
Occasional Contributor
Posts: 10

Re: Array

Please try codes below,

data have;
input jan feb mar apr may jun;
cards;
1 2 3 4 5 0
9 8 7 0 6 5
4 5 2 5 0 9
9 0 9 8 7 6
run;

data want(drop = i);
set have;
array mth _numeric_;
do i = 1 to dim(mth);
if mth(i) = 0 then tot = dim(mth)- i + 1;
end;
run;

cheers,
P.J
Respected Advisor
Posts: 4,173

Re: Array

Assuming all the numbers for months have only 1 digit the following would work as well:

data have;
input jan feb mar apr may jun;
cards;
1 2 3 4 5 0
9 8 7 0 6 5
4 5 2 5 0 9
9 0 9 8 7 6
run;

data want;
set have;
tot=find(cats(jun,may,apr,mar,feb,jan),'0');
run;

proc print data=want noobs;
run;
Respected Advisor
Posts: 3,799

Re: Array

The WHICHN function may be helpful, if the numbers are not single digits.

[pre]
data have;
input jan feb mar apr may jun;
cards;
1 2 3 4 5 0
9 8 77 0 6 51
4 5 32 5 0 99
9 0 99 8 7 6
run;

data want;
set have;
array _m
  • jun may apr mar feb jan;
    tot=find(cats(jun,may,apr,mar,feb,jan),'0');
    _0 = whichN(0,of _m
  • );
    run;
    proc print;
    run;
    run;
    [/pre]
  • Super User
    Posts: 10,023

    Re: Array

    After Patrick'code .


    [pre]
    data have;
    input jan feb mar apr may jun;
    cards;
    1 2 3 4 5 0
    9 8 7 0 6 5
    4 5 2 5 0 9
    9 0 9 8 7 6
    run;

    data want;
    set have;
    tot=indexc(cats(jan,feb,mar,apr,may,jun),'0',-1);
    run;

    proc print data=want noobs;
    run;
    [/pre]



    Ksharp
    Ask a Question
    Discussion stats
    • 4 replies
    • 153 views
    • 0 likes
    • 5 in conversation