BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ImSame
Calcite | Level 5

Hi all,

 

I have the following dataset,

 

RAW DATA

Var A

Agency #

ID #

Admission #

Service #

Service Type

Var Z

AAA

555

1

1

1

Phamacotherapy

YYY

AAA

555

1

1

2

Counseling

YYY

AAA

555

1

1

3

Counseling

YYY

BBB

999

10

5

1

Counseling

ZZZ

BBB

999

10

5

2

Rehabilitative Care

ZZZ

 

This dataset contains services utilized as part of admissions. Each admission is defined as the combination of Agency #, ID  # and Admission #. I want to count the services utilized as part of each admission, and ascertain if pharmacotherapy services were utilized as a part of the admission. The desired output should be as follows,

 

DESIRED OUTPUT

Var A

Agency #

ID #

Admission #

Service #

Service Type

Var Z

Total # of Services Utilized in Admission

Pharmacotherapy Part of Admission

AAA

555

1

1

1

Phamacotherapy

YYY

3

Yes

AAA

555

1

1

2

Counseling

YYY

3

Yes

AAA

555

1

1

3

Counseling

YYY

3

Yes

BBB

999

10

5

1

Counseling

ZZZ

2

No

BBB

999

10

5

2

Rehabilitative Care

ZZZ

2

No

 

Any help would be much appreciated.


Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Since you do not provide a problem with variable names specified, the below is basically a logic outline of two phases:

  1. Read and count all the rec's for a given admission.  Also check for presence of any pharmacotherepy services.
  2. With the count established (var TOTALSRV) and the PHARMPART var set to "Yes" or "No", reread and output each record.

 

data want;

  /* Read and count all recs for an admission */

  do totalsrv=1 by 1 until (last.admission);

    set have;

    by agency id admission;

    if svt_type='Pharmacotherapy' then pharmpart='Yes';

  end;

  if Pharmpart^='Yes' then Pharmpart='No';

  /* Reread and output */

  do until (last.admission);

    set have;

    by agency id admission;

    output;

  end;

run;

 

 

 

Notes:

  • This double-do-until-last technique is a common approach to getting group values assigned to individual group member records
  • It depends on the data set HAVE being sorted by the group vars (agency/id/admission in this case).
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

1 REPLY 1
mkeintz
PROC Star

Since you do not provide a problem with variable names specified, the below is basically a logic outline of two phases:

  1. Read and count all the rec's for a given admission.  Also check for presence of any pharmacotherepy services.
  2. With the count established (var TOTALSRV) and the PHARMPART var set to "Yes" or "No", reread and output each record.

 

data want;

  /* Read and count all recs for an admission */

  do totalsrv=1 by 1 until (last.admission);

    set have;

    by agency id admission;

    if svt_type='Pharmacotherapy' then pharmpart='Yes';

  end;

  if Pharmpart^='Yes' then Pharmpart='No';

  /* Reread and output */

  do until (last.admission);

    set have;

    by agency id admission;

    output;

  end;

run;

 

 

 

Notes:

  • This double-do-until-last technique is a common approach to getting group values assigned to individual group member records
  • It depends on the data set HAVE being sorted by the group vars (agency/id/admission in this case).
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1 reply
  • 744 views
  • 0 likes
  • 2 in conversation