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 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
    • 3 replies
    • 875 views
    • 0 likes
    • 2 in conversation