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]
  • sas-innovate-2026-white.png



    April 27 – 30 | Gaylord Texan | Grapevine, Texas

    Registration is open

    Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
    Register now and lock in 2025 pricing—just $495!

    Register now

    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
    • 1317 views
    • 0 likes
    • 2 in conversation