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

Dear SAS users, 

 

I need your help!  I would have to do a macro do loop and print the indext_date and an interval for each index time for each patient (-30 days and +30 days).

The patients in this table are unique and there is only one encounter per patient. 

 

Based on this I will have to match to controls using another macro.  

 

Can you please provide me with a code for the macro with do loops?

Here is an example of the table:

 

Encounter_idhospital_idAgepatient_idgender INDEX_dateDiabetes_event 
217566551F146111
427166572F146191
201619543M146201
281519354M146211
554666605M146291
445620626F146441

 

Thanks a lot!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Schtroumpfette
Obsidian | Level 7

Thank you both, 

This is helpful.  It would be to show the interval start and end.  

 

The reason I need a macro is that I will need to create a macro to select controls from another dataset (risk set sampling using macro), so I thought I start with a small dataset first.

 

The dataset here is a small sample of the cases.  I will have another dataset for controls, where I need to match on the index date interval, age(+/ 2 years), gender and hospital id. 

 

I can provide  an example of the control dataset.  Is there a macro code for risk set sampling?

Does this make sense?

 

Thanks,

Nisrine 

 

Thanks, 

View solution in original post

9 REPLIES 9
Reeza
Super User

I don't know that you need a macro. It would definitely help if you could explain what you're doing with a small data example. For starters, I don't think you need to do this to merge/join your data, you can do this type of join directly in SQL in the join clause.

 

I can see at least two interpretations of your question, one is to show the interval start and end and the other is to create an entry for each date in that interval. Both are shown below.

 

data want;
set have;

*figure out start/end of each interval;
interval_start = index_date - 30;
interval_end = index_date + 30;

*if you want a record for every date;
do date = interval_start to interval_end;
    output;
end;

run;

@Schtroumpfette wrote:

Dear SAS users, 

 

I need your help!  I would have to do a macro do loop and print the indext_date and an interval for each index time for each patient (-30 days and +30 days).

The patients in this table are unique and there is only one encounter per patient. 

 

Based on this I will have to match to controls using another macro.  

 

Can you please provide me with a code for the macro with do loops?

Here is an example of the table:

 

Encounter_id hospital_id Age patient_id gender  INDEX_date Diabetes_event 
2175 66 55 1 F 14611 1
4271 66 57 2 F 14619 1
2016 19 54 3 M 14620 1
2815 19 35 4 M 14621 1
5546 66 60 5 M 14629 1
4456 20 62 6 F 14644 1

 

Thanks a lot!

 


 

Schtroumpfette
Obsidian | Level 7

Thank you both, 

This is helpful.  It would be to show the interval start and end.  

 

The reason I need a macro is that I will need to create a macro to select controls from another dataset (risk set sampling using macro), so I thought I start with a small dataset first.

 

The dataset here is a small sample of the cases.  I will have another dataset for controls, where I need to match on the index date interval, age(+/ 2 years), gender and hospital id. 

 

I can provide  an example of the control dataset.  Is there a macro code for risk set sampling?

Does this make sense?

 

Thanks,

Nisrine 

 

Thanks, 

PaigeMiller
Diamond | Level 26

@Schtroumpfette wrote:

 

The reason I need a macro is that I will need to create a macro to select controls from another dataset (risk set sampling using macro), so I thought I start with a small dataset first.


As I understand things, no macro is needed. However, you seem to be avoiding giving us details about what this macro is supposed to do and how it related to the -30 days and +30 days you discussed earlier. Do you want to select controls withing this –30 days and +30 days window? Do you want to select ALL controls for a patient within this time period, or the FIRST control within this time period, or something else? Please explain.

 

 

--
Paige Miller
Schtroumpfette
Obsidian | Level 7
Hello Paige,

Yes, correct, I have a dataset with the cases. I will also have a large dataset for controls which includes the cases. I want to select controls within -30 to +30 days window of the index case of the date. It is a large dataset and if I do this step first, it will be very helpful in terms of future processing and this way I am matching also on calendar time. Once I select these controls, I will then match on age+/2 years and gender and hospital_id.
1. I want to select all controls within this time period
2. Match on age =/- 2 years, same gender and same hospital id
3. a control can become a case
4. A control id is not equal to case id not the same person
5. There will be an iterative loop that cycles through the first observation to the last observation in the case dataset
6. keep only four controls in dataset.
7. append all controls to case dataset.

Does that help?
Thanks
PaigeMiller
Diamond | Level 26

No macro needed. You can do this in a data step (if I am understanding you properly)

 

/* UNTESTED CODE */
data want;
    set have;
    index_date=index_date-30;
    output;
    index_date=index_date+60;
    output;
    format index_date mmddyy10.;
run;
proc print data=want;
run;
    

If you want tested code, please provide data in this format.

 

If that's not what you want, explain further and include examples of the desired output.

--
Paige Miller
Reeza
Super User
Three different interpretations of your question so far 🙂
Schtroumpfette
Obsidian | Level 7

Thank you, this is helpful. 

Reeza
Super User
Go read up on PROC PSMATCH or Google CDC "greedy matching macro". No need to reinvent the wheel.

Also, you can do this via SQL. If you really want a macro, first you need base code that works. Do you have that?
Schtroumpfette
Obsidian | Level 7

Thanks Reeza, 

 

I don't have a base code but I am working on potentially something based on this:  152-30: SAS® Programs to Select Controls for Matched Case-Control Studies

It is risk set sampling matching macro for Case Control studies.  Have you ever used this?

 

We opted out of PS matching.

 

I hope this helps.  

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1189 views
  • 2 likes
  • 3 in conversation