BookmarkSubscribeRSS Feed
R_Win
Calcite | Level 5
I should get the below output from the given dataset "X" .
data x;
input id a$ b$ c$ d$ e$ f$ g$;
cards;
1 B B B C C C P
3 B B C C C C P
4 C C C C C P P
5 B C C P P P P
6 P P P P P P P
7 C C C C P P P
run;


output:

id count(B) count(C) count(P)
1 3 3 1
3 2 4 1
4 0 5 2
....
.
. Message was edited by: Michel_SAS
3 REPLIES 3
R_Win
Calcite | Level 5
got it


data y(drop= a--g i);
set x;
array arr
  • _character_;
    bcount=0;Ccount=0;Pcount=0;
    do i=1 to dim(arr);
    select (arr);
    when ("B") Bcount=Bcount+1;
    when ("C") Ccount=Ccount+1;
    when ("P") Pcount=Pcount+1;
    end;
    end;
    run;
  • R_Win
    Calcite | Level 5
    data y(drop=x--r i );
    set x;
    bounce_string=cat(a,b,c,d,e,f,g);
    BCount=0;CCount=0;PCount=0;
    do i=1 to length(bounce_string);
    select(substr(bounce_string,i,1));
    when ("b") BCount=Bcount+1;
    when ("p") PCount=Pcount+1;
    when ("c") CCount=CCount+1;
    otherwise;
    end;
    end;
    run;
    proc print;
    run; Message was edited by: Michel_SAS
    data_null__
    Jade | Level 19
    Try the COUNT function.

    [pre]
    data x;
    input id a$ b$ c$ d$ e$ f$ g$;
    array v
  • a b c d e f g;
    string = cats(of v
  • );
    bCount = count(string,'B');
    cCount = count(string,'C');
    pCount = count(string,'P');
    cards;
    1 B B B C C C P
    3 B B C C C C P
    4 C C C C C P P
    5 B C C P P P P
    6 P P P P P P P
    7 C C C C P P P
    ;;;;
    run;
    proc print;
    run;
    [/pre]
  • 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
    • 3 replies
    • 1262 views
    • 0 likes
    • 2 in conversation