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

    SAS Innovate 2025: Save the Date

     SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

    Save the date!

    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
    • 1089 views
    • 0 likes
    • 5 in conversation