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

I have a table that includes a customer name and the difference (in months) between two dates. I need a macro that, based on the number of months in between the two dates, adds that determined amount of rows.

 

Basically, my table has 4 columns: Customer Name, Date 1, Date 2 and Months. Months refers to the difference in Months between Date 1 and Date 2. Again, my objective is to add rows below each customer. Number of rows to be added should depend on the number of "Months".

 

I tried building some code myself but ir takes forever to run. I am using SAS Enterprise Guide version 7.13

 

Looking for some smart suggestions on how I can create a more efficient method. Table has about 200,000 rows and last time I tried running it, it took more than 8 hours. Any thoughts?

 

Here's my attempt:

 

 

proc sql noprint;
select max(monotonic())
into : num_rows
from work.table_template
;
quit;

 

%put Number of rows is: &num_rows.;


options mprint merror mlogic symbolgen;

 

%macro one;

%do i = 1 %to &num_rows.;
data _null_;
set work.table_template;
if _n_ = &i.;
call symputx('Customer_Name',Customer_Name);
call symputx('Date1',Date1);
call symputx('Date2',Date2);
call symputx('Months',Months);
run;

%do j = 2 %to (&months.-1);
data _null_;
call symputx('Date1',intnx('month',&Date1.,1));
run;

 

proc sql;
insert into work.table_template
values ("&Customer_Name.",&Date1.,&Date2.,&Months.);
quit;
%end;
%end;

%mend one;

%one;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You're overcomplicating things by lightyears.

Maxim 11: A macro is not  needed.

Is true for close to 99.99% of cases with SAS beginners.

 

Consider this:

data want;
set have;
do i = 1 to months;
  output;
end;
run;

This will basically do the expand in one sweep.

Add custom calculated values in the do loop.

 

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

You're overcomplicating things by lightyears.

Maxim 11: A macro is not  needed.

Is true for close to 99.99% of cases with SAS beginners.

 

Consider this:

data want;
set have;
do i = 1 to months;
  output;
end;
run;

This will basically do the expand in one sweep.

Add custom calculated values in the do loop.

 

jlozano1
Calcite | Level 5

Thanks for the quick response. 

 

Worked perfectly. took about 15 seconds to run. I'll work on the custom values.

 

Thanks!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 2939 views
  • 1 like
  • 2 in conversation