BookmarkSubscribeRSS Feed
Haikuo
Onyx | Level 15

If you are only interested in the number of visits and the number of patients who have that visits, then Proc SQL also has approach: (raw data stoled from PG's post):

data visits;

length patient visit $10;

infile datalines missover;

input patient visit @;

do while (not missing(visit));

  output;

  input visit @;

  end;

datalines;

patientA clinic1 clinic2 clinic3 clinic1 clinic4 clinic1

PatientB clinic2 clinic3 clinic1 clinic2 clinic2

PatientC clinic1 clinic3 clinic1 clinic2 clinic2

;

proc sql;

  select ct as visits, count(*) as patient_num from

  (select count(*) as ct from visits group by patient)

group by ct;

quit;

Haikuo

SAS_new
Calcite | Level 5

Haikuo-

PROC SQL command seemed to have worked.  I gave an example for January, however, I am interested in getting counts for the remaining months.  Can you help me with the SQL coding on where I should include the variable 'month' to produce similar counts for each month?

Thank you

Haikuo
Onyx | Level 15

proc sql;

  select month, ct as visits, count(*) as patient_num from

  (select count(*) as ct, month from visits group by month,patient)

group by month,ct;

quit;

Not tested.

Haikuo

SteveNZ
Obsidian | Level 7

Probably more elegant ways but:

data have;

length month patient visit $10;

infile datalines missover;

input month patient visit @;

do while (not missing(visit));

     output;

     input visit @;

     end;

datalines;

January patientA clinic1 clinic2

January PatientB clinic2 clinic3 clinic1 clinic2 clinic2

January PatientC clinic1 clinic3 clinic1 clinic2 clinic2 clinic3

February patientA clinic1 clinic2 clinic3 clinic1 clinic4 clinic1

February PatientB clinic2 clinic3 clinic1 clinic2 clinic2

February PatientC clinic1 clinic3 clinic1 clinic2 clinic2

;

    proc summary noprint nway n

        data = have ;

                class month patient ;

        output out = tmp_want (

                        drop = _type_

                        rename = (_freq_ = visits)) ;

    run ;

    proc summary noprint nway n

        data = tmp_want ;

                class month visits ;

        output out = want (

                        drop = _type_

                        rename = (_freq_ = patients)) ;

    run ;

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
  • 18 replies
  • 3876 views
  • 2 likes
  • 6 in conversation