Hello,
I have two separate macro results (macro for janitor and macro for baker) and
my aim is to merge the two datasets together, but they are not working.
Data set for janitor macro is jan and that for baker is bake.
Table 1 is janitor results and Table 2 is baker results (shown below).
proc print data=jan; doesn't work; same with proc print data=bake;
So to print janitor records, I just do proc print; and it works; same with baker.
Help is needed, please to merge dataset jan and dataset bake, and to be merged
finally with the original data Table o.
[I did not run the merge procedures as they would print the 17 records (small_t_data)
My data, code and results are found below:
Thanks in advance. [ No errors in log-too long so not shown]
ak.
[As usual, I have read the SAS community answers & SAS documentation on macros but still no solution
found]. I would appreciate your help, please.
/**MACRO*/
data full_tdata;
input id$ 1-6 job 7-8 jobyrin 9-11 jobyrout 18-20 icode$ 23-27 lung$ 29-35;
datalines;
OSa13 3 73 78 6191 cacase
OSa30 1 39 46 7181 cacase
OSa30 3 56 64 6191 cacase
OSa73 1 23 31 7181 popcon
OSa73 2 31 42 5130 popcon
OSa86 3 46 60 6198 cacon
OSa86 4 60 70 5130 cacon
OSa93 3 68 72 6121 popcon
OSf26 1 54 54 6198 popcon
OSf26 2 70 70 6191 popcon
OSh77 1 63 66 6121 cacon
OSh77 2 66 70 6121 cacon
OSh77 3 70 71 6121 cacon
OSi84 1 67 75 6191 cacase
OSi84 2 75 81 6191 cacase
OSj17 3 60 75 6191 cacase
OSj17 4 65 70 6191 cacase
;
proc print data=full_tdata;
title 'Table O: Original full_tdata file'; run;
data small_tdata; set full_tdata; drop lung;
*max 4 jobs per subject;
%macro duration(database=small_tdata, icode=6191, Job_title=janitor);
*Creating a dataset containing all the jobs with selected icode;
data &Job_title;
set &database;
if icode = &icode;
run;
*transposing dataset so that the YEARIN and YEAROUT of all jobs held by a subject are in the same row;
*YEARIN;
proc transpose data = &Job_title out = &Job_title._yearin prefix = YEARIN;
by ID;
var jobyrin;
run;
proc sort data = &Job_title._yearin;
by id;
run;
*YEAROUT;
proc transpose data = &Job_title out = &Job_title._yearout prefix = YEAROUT;
by ID;
var jobyrout;
run;
proc sort data = &Job_title._yearout;
by id;
run;
data &Job_title._final;
merge &Job_title._yearin &Job_title._yearout;
by ID;
array YEARIN (3) YEARIN1 YEARIN2 YEARIN3;
array YEAROUT (3) YEAROUT1 YEAROUT2 YEAROUT3;
array YEARINv2 (3) YEARIN2 YEARIN3 YEARIN4 ;
array YEAROUTv2 (3) YEAROUT2 YEAROUT3 YEAROUT4;
array YEARIN_F (4) YEARIN1 YEARIN2 YEARIN3 YEARIN4;
array YEAROUT_F (4) YEAROUT1 YEAROUT2 YEAROUT3 YEAROUT4;
Array duration (4) Dur1 Dur2 Dur3 Dur4;
do i = 1 to 3;
if yearin(i) ne . and yearinv2(i) ne . and yearinv2(i) le yearout(i) and yearoutv2(i) ge yearout(i) then do; yearinv2(i) = yearin(i); yearin(i) = .;yearout(i) = .;end;
else if yearin(i) ne . and yearinv2(i) ne . and yearinv2(i) le yearout(i) and yearoutv2(i) lt yearout(i) then do; yearinv2(i) = yearin(i);yearoutv2(i) = yearout(i); yearin(i) = .;yearout(i) = .;end;
end;
do i = 1 to 4;
if YEARIN_F(i) = . then YEARIN_F(i) = 0;
if YEAROUT_F(i) = . then YEAROUT_F(i) = 0;
duration(i) = YEAROUT_F(i) - YEARIN_F(i);
if YEARIN_F(i) ne 0 and YEAROUT_F(i) = YEARIN_F(i) then duration(i) = 0.5;
end;
&Job_title._dur = Dur1 + Dur2 + Dur3 + Dur4;
&Job_title._ever = 1;
keep ID &Job_title._dur &Job_title._ever;
run;
%MEND duration;
/* 1st macro-janitor icode 6191*/
data jan; set small_tdata;
%duration(database=small_tdata, icode=6191, Job_title=janitor);
proc print;
title 'Table 1: janitor records';
run;
proc sort data=jan; by id;
/* 2nd macro-baker icode 7181*/
data bake; set small_tdata;
%duration(database=small_tdata,icode=7181, Job_title=baker);
proc print;
title 'Table 2: baker records';
run;
OSa13 |
3 |
73 |
78 |
6191 |
cacase |
OSa30 |
1 |
39 |
46 |
7181 |
cacase |
OSa30 |
3 |
56 |
64 |
6191 |
cacase |
OSa73 |
1 |
23 |
31 |
7181 |
popcon |
OSa73 |
2 |
31 |
42 |
5130 |
popcon |
OSa86 |
3 |
46 |
60 |
6198 |
cacon |
OSa86 |
4 |
60 |
70 |
5130 |
cacon |
OSa93 |
3 |
68 |
72 |
6121 |
popcon |
OSf26 |
1 |
54 |
54 |
6198 |
popcon |
OSf26 |
2 |
70 |
70 |
6191 |
popcon |
OSh77 |
1 |
63 |
66 |
6121 |
cacon |
OSh77 |
2 |
66 |
70 |
6121 |
cacon |
OSh77 |
3 |
70 |
71 |
6121 |
cacon |
OSi84 |
1 |
67 |
75 |
6191 |
cacase |
OSi84 |
2 |
75 |
81 |
6191 |
cacase |
OSj17 |
3 |
60 |
75 |
6191 |
cacase |
OSj17 |
4 |
65 |
70 |
6191 |
cacase |
OSa13 |
5.0 |
1 |
OSa30 |
8.0 |
1 |
OSf26 |
0.5 |
1 |
OSi84 |
14.0 |
1 |
OSj17 |
15.0 |
1 |
Tables 1 and 2 results are what I am expecting but the problem is how to merge them.
Thanks in advance for your help.
ak.