BookmarkSubscribeRSS Feed
AbuYusuf
Calcite | Level 5

Hi,

 

I am having trouble with something that appears pretty simple to do. I have administrative data on individual patients identified by ID, date of event (year-month-day) and the two types of events, A and B. What I want to do is to figure out if event B tends to follow event A. I sorted the data by ID and date, and then I tried to use a simple do loop. I don't really know how to use loops all that well, but my understanding is that I had to create an array first.:

 

 

indics = 0;   *indicator variable

array ed {1} A;

array psych {1} B;

do a = 1 to 1181047;

if ed[a] = 1 & psych[a+1] = 1 then indics = 1;

end;

run;

 

I get an error saying that index is out of bounds. I tried using the number of non-missing values as the total number of iterations, but it still didn't work. I am wondering if this approach can work in theory, or should I try something else?

 

thank you

6 REPLIES 6
SASKiwi
PROC Star

Please post some sample data in a DATA step. This type of logic can be easily solved without arrays.

AbuYusuf
Calcite | Level 5

Thank you very much for your response. Will post some sample data in a response below.

mkeintz
PROC Star

 

An array is intended to treat a collections of variables within a single observation - i.e. typically within a "row" of data.  But your description of the data suggests you have multiple observations ( 1 per event) for each individual.

 

Please show a sample, in the form of a SAS data set with actual data, of what your dataset looks like, and what you would want the analysis result to look like.

 

 

--------------------------
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

--------------------------
AbuYusuf
Calcite | Level 5

Thank you for your help! I will post some sample data below. But you are right, I have administrative hospital data on patients moving between hospitals. There are unique IDs, but there may be multiple observations per individual. I am interested if Event2  follows Event1. I'd like to create a dichotomous variable that equals 1 if event2 follows event1 and 0 otherwise.

ballardw
Super User

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

 

Also what gets done when two sequential events are the same?

Are there ever more than 2 records? How would that effect the output?

You really should show what you expect the indicator to actually look like for given results.

 

And if you have data as separate records per event then a variable should indicate which type of record is involved. Something like this:

data example;
   input id date :date9. event $;
   format date date9.;
datalines;
1 02Feb1966 A
1 14Mar1967 B
2 18Jun1988 B
2 23Aug1989 A
2 18Sep1989 A
2 18Oct1989 B
;

proc sort data=example;
  by id date;
run;

data want;
  set example;
  by id;
  levent = lag(event);
  if not first.id and event ne levent then eventsequence = catx('>',levent, event);
  drop levent;
run; 

The above also shows an example of how to post a data set as data step code.

AbuYusuf
Calcite | Level 5

Thank you very much for your response and the code. Here is sample data:

 

data WORK.SAMPLEDATA;

infile datalines dsd truncover;

input ID:BEST12. Date:$13. Event1:BEST12. Event2:BEST12.;

format ID BEST12. Event1 BEST12. Event2 BEST12.;

datalines;

1 19/Oct/2018 0 0

1 01/Dec/2018 1 1

1 12/Jan/2018 0 1

2 01/Sep/2018 0 1

2 05/Nov/2018 1 1

2 03/Feb/2018 0 0

2 01/Mar/2018 0 1

2 19/Sep/2018 0 0

3 08/Sep/2018 0 0

3 04/Apr/2018 0 1

3 14/Apr/2018 1 1

4 20/Apr/2018 1 1

4 04/Jun/2018 0 0

5 11/Jun/2018 0 1

5 21/Jun/2018 0 0

;;;;

 

I think the code you provided is very close to what I'd like to do. It's just I have 2 variables instead of 1.

 

Thank you!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 6 replies
  • 551 views
  • 0 likes
  • 4 in conversation