DEV Community

Judy
Judy

Posted on

SQL, get historical status of each ID #eg22

In the sas system, table tab stores the patients’ medical history. Visit_code=Surgery means that the patient is in surgery; Visit_code=Office means the patient is in consultation room. Each patient has more than one Visit_code; one may only have Surgery or Office. Below is the table:

Image description
Task: Get the historical medical status for each patient. If they once got consultation, record Office_Visit as 1 (Office_Visit=1), otherwise record it as 0; if they got consultation six months after their surgery, record SX_past_6mo as 1 (SX_past_6mo=1), otherwise record it as 0:

Image description
Write the following SPL code:

Image description
A1: Retrieve data from sas through JDBC and sort data by Visit_code and Visit_Date.

A2: Group data by Id and create a new two-dimensional table based on the groups. ~ is the current group, Visit_code[-1] represents Visit_code field of the previous record; interval@m gets the number months between two dates.
Source: https://stackoverflow.com/questions/78443712/indicate-whether-a-date-is-6-months-before-another-date-for-the-same-id-column

Top comments (1)

Collapse
 
esproc_spl profile image
Judy

SPL open source address:github.com/SPLWare/esProc/stargazers