BookmarkSubscribeRSS Feed
sas_
Fluorite | Level 6
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.
4 REPLIES 4
P_J
Calcite | Level 5 P_J
Calcite | Level 5
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
Patrick
Opal | Level 21
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;
data_null__
Jade | Level 19
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]
  • Ksharp
    Super User
    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

    hackathon24-white-horiz.png

    The 2025 SAS Hackathon has begun!

    It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

    Latest Updates

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

    Find more tutorials on the SAS Users YouTube channel.

    SAS Training: Just a Click Away

     Ready to level-up your skills? Choose your own adventure.

    Browse our catalog!

    Discussion stats
    • 4 replies
    • 1567 views
    • 0 likes
    • 5 in conversation