SASUnit Examples  Version 1.5.0
_dependency_wr.sas
Go to the documentation of this file.
1 
24 %MACRO _dependency_wr(i_node=
25  ,i_dependencies=
26  ,i_direction=
27  ,i_parentList=
28  );
29 
30  %LOCAL l_child l_children l_cntChildren l_i l_node l_direction l_column l_parentList l_len l_this_node l_children_inner;
31  %LET l_children =;
32 
33  /* handle direction */
34  %IF &i_direction EQ 1 %THEN %DO;
35  %LET l_direction = called;
36  %LET l_column = caller;
37  %END;
38  %ELSE %DO;
39  %LET l_direction = caller;
40  %LET l_column = called;
41  %END;
42 
43  /* get children of node*/
44  PROC SQL noprint;
45  select distinct &l_direction
46  into :l_children separated by ' '
47  from &i_dependencies
48  where lowcase (&l_column)="%lowcase(&i_node.)"
49  ;
50  QUIT;
51 
52  /* Write data nodes */
53  DATA _NULL_;
54  FILE json_out mod;
55  put '{ "name": "' "&i_node" '"';
56  RUN;
57 
58  %IF "&l_children" NE "" %THEN %DO;
59  /* prepare loop over all children */
60  %LET l_cntChildren = %SYSFUNC(countw("&l_children."));
61  %DO l_i=1 %TO &l_cntChildren.;
62  %LET l_node=%SYSFUNC(SCAN(&l_children,&l_i));
63  %LET l_parentList = &i_parentList. &l_node.;
64 
65  /* Check for self-referential loops in call hierarchy like A -> B -> A */
66  %LET l_len = %SYSFUNC(COUNTW(&l_parentList.));
67  %LET l_this_node = %SYSFUNC(SCAN(&l_parentList., &l_len));
68  %DO l_j=1 %TO %EVAL(&l_len. - 1);
69  %IF "%sysfunc(scan(&i_parentList., &l_j))" EQ "&l_this_node." %THEN %DO;
70  /* Return if self referential loop found */
71  %RETURN;
72  %END;
73  %END;
74 
75  %IF &l_i EQ 1 %THEN %DO;
76  DATA _NULL_;
77  FILE json_out mod;
78  PUT ', "children": [';
79  RUN;
80  %END;
81 
82  /* get children of node*/
83  PROC SQL noprint;
84  select distinct &l_direction
85  into :l_children_inner separated by ' '
86  from &i_dependencies
87  where lowcase (&l_column)="%lowcase(&i_node.)"
88  ;
89  QUIT;
90 
91  /* Child node found: recursive call to macro */
92  %_dependency_wr(i_node=&l_node., i_dependencies=&i_dependencies., i_parentList=&l_parentList., i_direction=&i_direction.);
93 
94  /* Separate children with curly bracket + comma */
95  %IF &l_i LT &l_cntChildren. %THEN %DO;
96  DATA _NULL_;
97  FILE json_out mod;
98  PUT '},';
99  RUN;
100  %END;
101  /* Last Child: only curly bracket needed */
102  %ELSE %DO;
103  DATA _NULL_;
104  FILE json_out mod;
105  PUT '}';
106  RUN;
107  %END;
108 
109  %END; /* End do i to l_cntChildren */
110  /* Add closing bracket after last child */
111  DATA _NULL_;
112  FILE json_out mod;
113  PUT "]";
114  RUN;
115 
116  %END; /* End if */
117 %MEND _dependency_wr;