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: Call for Content

    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!

    Submit your idea!

    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.

    Click image to register for webinarClick image to register for webinar

    Classroom Training Available!

    Select SAS Training centers are offering in-person courses. View upcoming courses for:

    View all other training opportunities.

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