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

Dear experts,

 

here my input data:

1. a data set where for each id I specify the time period with begin and end (YYYMM)

 

data T00_00_PTF;
input reference_period: $12. begin: comma5. end: comma5.;
datalines;
current_month 201606 201606
previous_month 201605 201605
previous_12m 201507 201606
previous_36m 201307 201606
;

data T00_00_PTF;
set T00_00_PTF;
id=cats(reference_period,"_",begin,"_",end)
;run;

 

2. a data set of row data where the field date define the YYYYMM: 

 

data have;
input date: comma5. revenue: comma5. shop: $12. country: $2.;
datalines;
201606 10000 ABC UK
201606 20000 A JP
201605 30000 A JP
201508 40000 A JP
201605 30000 BC UK
201308 20000 BC UK
;

 

on the base of the table T00_00_ptf I would like create another data set have which is expanding the initial data set have, adding to each observation the field id from the data set T00_00_ptf in the case that the variable data is in the time range. Here the output that I expect for the first observation (date=201606 => included in all the subset defined in the table T00_00_PTF)

 

data want;
input id: $25. date: comma5. revenue: comma5. shop: $12. country: $2.;
datalines;
current_mont_201606_201606 201606 10000 ABC UK
previous_mon_201605_201605 201606 10000 ABC UK
previous_12m_201507_201606 201606 10000 ABC UK
previous_36m_201307_201606 201606 10000 ABC UK
;

 

Any suggestion? I tried using a proc sql with the left outer join (left outer join T00_00_ptf t5. on (t5.begin=<t0.date and t0.date<=t5.end) but actually I would like to run it with a macro, so I can use it in the future also for different purposes.

Thanks a lot, SH. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

The solution you describe seems OK to me. Since you do not indicate which other purposes you want to

use your program for, it is difficult to give you an answer.

Also, i think previous_month should not be included in your want dataset since begin and end are 201605.

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

If you already have working code, then you just need to parameterise that code to make a macro:

proc sql;
  create table WANT as 
  select * from SASHELP.CARS;
quit;

Could become:

%macro Create_Table (ds=,frm=);
  proc sql;
    create table &DS. as
    select * from &FRM.;
  quit;
%mend Create_Table;

%Create_Table (ds=WANT,frm=SASHELP.CARS);

However, in my experience, macro code merely complicates processes and in 99% of cases is not worth it in the first place, as SAS Base has inbuilt functionality to do things.

gamotte
Rhodochrosite | Level 12

The solution you describe seems OK to me. Since you do not indicate which other purposes you want to

use your program for, it is difficult to give you an answer.

Also, i think previous_month should not be included in your want dataset since begin and end are 201605.

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
  • 897 views
  • 0 likes
  • 3 in conversation