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

I need to repeat a sas datastep using macro. I have two sets of dates, stimulant prescription dates "stim_1" to "stim_190" and opioid prescription dates "_1" to "_825".  I am interested to see if these prescriptions are concurrent prescriptions - if they are within 30 days of each other. So, I have written the following code to calculate:

 

data opi.concurrent_test;
set opi.concurrent_merged;

concurrent_1 = 0;

array bupe [825] _1 - _825;

if not missing (stim_1) then do i = 1 to 825;
concurrent_1 = (0 le (stim_1- bupe (i)) le 30);

if concurrent_1 = 1 then leave;
end;

drop i;

if (_1 - _825) | stim_1 = . then concurrent_1 = .;

if concurrent_1 = 1 then concurrent_1 = year(stim_1);

run;

 

But now, I need to run this for all 190 stimulant dates. How can I write a macro to repeat this procedure 190 times. 

 

Thank you! 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star
data opi.concurrent_test;
set opi.concurrent_merged;

concurrent_1 = 0;

array bupe [825] _1 - _825;
array stims [*]  stim_1 - stim_100;
array concurrents [*]  concurrent_1 - concurrent_100;

do j = 1 to dim(stims);

if not missing (stims(j)) then do i = 1 to 825;
concurrents(j) = (0 le (stims(j) - bupe (i)) le 30);

if concurrents(j) = 1 then leave;
end;

end;
drop i;

if (_1 - _825) | stim_1 = . then concurrent_1 = .;

if concurrent_1 = 1 then concurrent_1 = year(stim_1);

run;

Something like the above.

View solution in original post

6 REPLIES 6
Reeza
Super User

I suspect your data structure is problematic here... can you please post example data?

Fake data is fine. It would also help if you illustrated how you would do this for two drugs, even if it's entirely separate so we can see where the code needs to change. 

 

Here are some generic references to help you get started.

UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/

Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

Examples of common macro usage
https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

 


@Tithi wrote:

I need to repeat a sas datastep using macro. I have two sets of dates, stimulant prescription dates "stim_1" to "stim_190" and opioid prescription dates "_1" to "_825".  I am interested to see if these prescriptions are concurrent prescriptions - if they are within 30 days of each other. So, I have written the following code to calculate:

 

data opi.concurrent_test;
set opi.concurrent_merged;

concurrent_1 = 0;

array bupe [825] _1 - _825;

if not missing (stim_1) then do i = 1 to 825;
concurrent_1 = (0 le (stim_1- bupe (i)) le 30);

if concurrent_1 = 1 then leave;
end;

drop i;

if (_1 - _825) | stim_1 = . then concurrent_1 = .;

if concurrent_1 = 1 then concurrent_1 = year(stim_1);

run;

 

But now, I need to run this for all 190 stimulant dates. How can I write a macro to repeat this procedure 190 times. 

 

Thank you! 


 

Tithi
Calcite | Level 5

Attached you will find a fake dataset. In my real dataset, I have 190 stimulant prescription date variables and 825 opioid prescription date variables over the course of 10 years. I need to subtract each stimulant prescription date with all the opioid prescription dates to find concurrent prescription. Please let me know if there is a better way to approach this coding. Thank you! 

SASKiwi
PROC Star

You don't need macro. Just add another array for your stimulant prescription dates and a second loop inside your first loop to process these.

Tithi
Calcite | Level 5
I think I am struggling to write a second loop with a different dimension. Do you have an example? Thank you!
SASKiwi
PROC Star
data opi.concurrent_test;
set opi.concurrent_merged;

concurrent_1 = 0;

array bupe [825] _1 - _825;
array stims [*]  stim_1 - stim_100;
array concurrents [*]  concurrent_1 - concurrent_100;

do j = 1 to dim(stims);

if not missing (stims(j)) then do i = 1 to 825;
concurrents(j) = (0 le (stims(j) - bupe (i)) le 30);

if concurrents(j) = 1 then leave;
end;

end;
drop i;

if (_1 - _825) | stim_1 = . then concurrent_1 = .;

if concurrent_1 = 1 then concurrent_1 = year(stim_1);

run;

Something like the above.

Tithi
Calcite | Level 5
Thank you so much!

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1064 views
  • 2 likes
  • 3 in conversation