countsasfieldrecord

SAS - Count how many times a value appear in a field for each record


I have the following SAS dataset, with a single columns called STAT (This field contains a series of values for each record):

enter image description here

I need to create a new field called CountOpen that, for each row, counts how many times the word Open is contained in the field STAT. So, from the example above, the resulting dataset would be:

enter image description here

Can someone help me, please?


Solution

  • I think that the count function does what you are looking for:

    *Replicate data;
    
    data dt;
    infile cards dlm = ",";
    length stat $30;
    input stat;
    cards;
    Closed Open Open,
    Closed Closed Open,
    Open,
    Open,
    ;
    run;
    
    *Compute countOpen using count;
    
    data dt2;
    set dt;
    countOpen = count(stat, "Open");
    run;
    
    proc print data = dt2;
    run;
    

    enter image description here