Loading...
Searching...
No Matches
mpe_makesampledata.sas
Go to the documentation of this file.
1/**
2 @file mpe_makesampledata.sas
3 @brief Creates the sample data library DC_DEMO
4 @details
5 Called by makedata when the deployment asked for sample data - the SAMPLEDATA
6 flag the deploy screen offers, off by default. It creates `<path>/dcdemo`,
7 assigns it as DC_DEMO, writes the sample tables there and registers them in
8 MPE_TABLES, so a new deployment has something to browse, filter and edit
9 without pointing DC at a site's own data.
10
11 It is built only when the caller passes `iftrue=Y` - the value the deploy
12 screens, the seed files and the configurator all send - so an absent or
13 unrecognised flag cannot create the library.
14
15 Some of the tables are generated from a fixed rule, not copied from SASHELP,
16 for two reasons: SASHELP is not the same on every platform, and the JS estate
17 (sas/mocks) has no SASHELP at all. The mock's mpeMakesampledata writes those
18 same tables from the same rule, and sas/mocks/test/makedata-sampledata.test.js
19 compares the two on the table list and the registrations - a change here has
20 to be made there.
21
22 The rule for the generated tables (n is the row number, 1-based):
23
24 @li STUDENTS - 20 rows: NAME=student nn, SEX=M on odd n, AGE=12+mod(n,7),
25 HEIGHT=50+n, WEIGHT=80+n
26 @li VEHICLES - 30 rows: MAKE cycles Audi/BMW/Ford/Honda/Toyota,
27 MODEL=Model nn, TYPE cycles Sedan/SUV/Wagon, ORIGIN=Europe for Audi and
28 BMW else Asia, DRIVETRAIN=Front on odd n, MSRP=20000+n*750
29 @li FORMATS - 1 row: PK=1, E8601DA=01JAN2026, DTM=01JAN2026:13:45:00
30
31 The dates are recorded as their SAS values (24107 and 2082894300), which is
32 what the tables hold, so the two estates can be compared row for row.
33
34 The fixtures after them are defined by their own data steps rather than by
35 the rule, so the list of tables this macro writes is the data steps in the
36 body, and the comment above each one says what it is for.
37
38 DC_DEMO is assigned here and cleared again: the caller writes the assignment
39 into its own settings from the same flag it passed in, so the library and the
40 config line cannot disagree about whether it is there.
41
42 @param lib= The control library - mpe_tables is updated here.
43 @param path= The DC location. The library is created below it, as dcdemo.
44 @param iftrue= The caller's flag, Y or N and defaulting to N: the library is
45 built only when it is Y. Fails closed, so a call that forgets the argument
46 - or a site that has not opted in - cannot create it.
47
48 <h4> SAS Macros </h4>
49 @li mf_mkdir.sas
50 @li mf_trimstr.sas
51 @li mp_abort.sas
52
53 @version 9.2
54 @author 4GL Apps Ltd
55 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
56 and may not be re-distributed or re-sold without the express permission of
57 4GL Apps Ltd.
58
59 @sasjslint {"maxLineLength": 80}
60
61**/
62
63%macro mpe_makesampledata(lib=,path=,iftrue=N);
64%if &syscc ne 0 %then %return;
65
66%if %superq(lib)=%str() %then %do;
67 %put NOTE: &sysmacroname needs LIB - no sample data created;
68 %return;
69%end;
70%if %superq(path)=%str() %then %do;
71 %put NOTE: &sysmacroname needs PATH - no sample data created;
72 %return;
73%end;
74
75/* ---------------- the sample data library ---------------- */
76%if "&iftrue" ne "Y" %then %return;
77
78%local demo_path;
79%let demo_path=%mf_trimstr(&path,/)/dcdemo;
80
81%mf_mkdir(&demo_path)
82%mp_abort(iftrue= (&syscc ne 0)
83 ,mac=&_program
84 ,msg=%str(Unable to create &demo_path using &sysuserid)
85)
86
87libname dcdemo "&demo_path";
88%mp_abort(iftrue= (&syscc ne 0)
89 ,mac=&_program
90 ,msg=%str(Unable to assign DC_DEMO to &demo_path)
91)
92
93/* STUDENTS - the small all-purpose table */
94data dcdemo.students;
95 length name $20 sex $1;
96 do id=1 to 20;
97 name='student '!!put(id,z2.);
98 if mod(id,2) then sex='M';
99 else sex='F';
100 age=12+mod(id,7);
101 height=50+id;
102 weight=80+id;
103 output;
104 end;
105run;
106
107/* VEHICLES - a list of values, for filters and dropdowns */
108data dcdemo.vehicles;
109 length make $10 model $12 type $10 origin $6 drivetrain $5;
110 array _makes[5] $10 _temporary_
111 ('Audi' 'BMW' 'Ford' 'Honda' 'Toyota');
112 array _types[3] $10 _temporary_ ('Sedan' 'SUV' 'Wagon');
113 do id=1 to 30;
114 make=_makes[1+mod(id-1,5)];
115 model='Model '!!put(id,z2.);
116 type=_types[1+mod(id-1,3)];
117 if make in ('Audi','BMW') then origin='Europe';
118 else origin='Asia';
119 if mod(id,2) then drivetrain='Front';
120 else drivetrain='Rear';
121 msrp=20000+(id*750);
122 output;
123 end;
124run;
125
126/* FORMATS - one row carrying the date formats the viewer renders */
127data dcdemo.formats;
128 pk=1;
129 e8601da=24107;
130 dtm=2082894300;
131 format e8601da e8601da10. dtm e8601dt19.;
132run;
133
134
135/* -------------------------------------------------------------------------
136 The demo/test tables
137
138 Everything below is a table makedata does NOT create on its own: the
139 fixtures a deployment needs to see the app work - a deep table, a wide
140 one, validation and formula examples, an empty mirror beside a real
141 table, a derived-variable example. They live in DC_DEMO with the generated
142 tables above, so ONE SAMPLEDATA run gives a fresh deployment the whole
143 surface and no fixture table is parked in the control library.
144
145 The JS estate builds the same library: the generated tables in
146 sas/mocks/sasjs/services/admin/makedata.js and these fixtures in
147 demodataTables.js beside it. sas/mocks/test/makedata-sampledata.test.js
148 compares the two on the table list and the registrations, so a table added
149 here has to be added there. The VALUES differ, which is fine for a test
150 library: the Cypress suite asserts the mock's rows, which come from its own
151 rotation of sites and vessels, so a SAS-seeded demo site shows the same
152 tables with plainer data. The rule rows the mock's fixtures rely on -
153 validations, selectbox options, derived variables - are mock fixtures too:
154 this macro writes the tables and their registrations, and the DQ and formula
155 demos are inert on a SAS-seeded site. sas/mocks/test/parity/README.md records
156 both with the other divergences.
157 ------------------------------------------------------------------------- */
158
159/* MPE_X_SEARCH - the viewer's full-table search demo, 1000 rows */
160data dcdemo.mpe_x_search;
161 length site_name $32 vessel $32 species $32 expedition_id $12
162 notes $512;
163 attrib primary_key_field label='Primary Key'
164 cruise_date label='Cruise Date' format=date9.
165 sample_count label='Samples Collected'
166 depth_m label='Depth (m)';
167 do primary_key_field=1001 to 2000;
168 site_name='Nereid Trench '!!put(primary_key_field-1000,z4.);
169 vessel='RV Pelagia';
170 sample_count=1+mod(primary_key_field,5);
171 depth_m=primary_key_field;
172 species='Vampire squid';
173 cruise_date=20000+mod(primary_key_field,60);
174 expedition_id='EXP-'!!put(primary_key_field-1000,z4.);
175 notes='Leg 1 of the Nereid Trench survey. No anomalies recorded.';
176 output;
177 end;
178run;
179
180/* MPE_X_WIDE - 1001 columns, for the EDIT cell limit */
181data dcdemo.mpe_x_wide;
182 array _c[500] $1 char1-char500;
183 array _n[500] 8 num1-num500;
184 do row_id=1 to 200;
185 do _i=1 to 500;
186 _c[_i]=byte(65+mod(row_id+_i,26));
187 _n[_i]=row_id*1000+_i;
188 end;
189 output;
190 end;
191 drop _i;
192run;
193
194/* DEMO_01 - eight columns covering the rule types, incl. a special missing */
195data dcdemo.demo_01;
196 attrib id label='ID'
197 amount label='Amount'
198 score label='Score'
199 grade label='Grade'
200 ref label='Reference'
201 rating label='Rating'
202 status label='Status'
203 last_reviewed label='Last Reviewed' format=date9.;
204 do id=1 to 8;
205 amount=120*id;
206 score=70+id;
207 if id=1 then grade=.a;
208 else grade=2+mod(id,4);
209 ref=1000+id;
210 rating=1+mod(id,5);
211 status=mod(id,2);
212 last_reviewed=23389+id;
213 output;
214 end;
215run;
216
217/* DEMO_ORDERS, and an empty mirror with the same schema. The mirror's
218 pre/post-edit hooks are JS-only: a SAS deployment has no services/hooks
219 programs behind them, so they are not registered here. */
220data dcdemo.demo_orders;
221 length customer $20 region $8 status $12;
222 attrib order_id label='Order Id' amount label='Amount';
223 do order_id=1001 to 1004;
224 customer=scan('Acme Corp|Globex|Initech|Umbrella',1+mod(order_id,4),'|');
225 region=scan('EU|US|APAC',1+mod(order_id,3),'|');
226 amount=450*mod(order_id,4)+100;
227 status=scan('NEW|OPEN|SHIPPED',1+mod(order_id,3),'|');
228 output;
229 end;
230run;
231
232data dcdemo.demo_mirror;
233 length customer $20 region $8 status $12;
234 attrib order_id label='Order Id' amount label='Amount';
235 /* assign then stop: the columns come from the LENGTH and ATTRIB above, so
236 a bare STOP would leave the same five, but assigning keeps the values
237 explicit rather than relying on the reader to know that */
238 order_id=.; amount=.; customer=''; region=''; status='';
239 stop;
240run;
241
242/* DERIVED_DEMO - the derived-variable example */
243data dcdemo.derived_demo;
244 length state $2;
245 attrib id label='ID' state label='State' qty label='Quantity'
246 price label='Price' format=dollar12.2;
247 do id=1 to 4;
248 state=scan('CA|NY|TX|WA',id,'|');
249 qty=id*2;
250 price=10.5*id;
251 output;
252 end;
253run;
254
255/* MPE_X_FORMULA_PK_TEST - two rows, a hard and a soft formula in the key */
256data dcdemo.mpe_x_formula_pk_test;
257 length pk_softformula_col $128;
258 do pk_hardformula_col=1 to 2;
259 pk_softformula_col='soft-'!!put(pk_hardformula_col,1.);
260 a_col=pk_hardformula_col;
261 b_col=pk_hardformula_col+1;
262 output;
263 end;
264run;
265
266/* MPE_X_FORMULA_TEST - the formula columns, 10 rows */
267data dcdemo.mpe_x_formula_test;
268 length formula_hard_col $128 formula_soft_col $128
269 row_status_col $128 user_name_col $128 orig_value_col $128
270 change_summary_col $128 plain_text_col $128;
271 /* the four formula columns are deliberately empty - a formula owns them -
272 but a declared variable that is never assigned fails the varinitchk check
273 mp_init sets, and the step then stops with no rows written at all */
274 call missing(of formula_hard_col formula_soft_col row_status_col
275 user_name_col);
276 do primary_key_field=1 to 10;
277 a_col=primary_key_field;
278 b_col=10*primary_key_field;
279 orig_value_col='orig-'!!put(primary_key_field,1.);
280 change_summary_col='orig-'!!put(primary_key_field,1.);
281 plain_text_col='note-'!!put(primary_key_field,1.);
282 output;
283 end;
284run;
285
286/* MPE_X_NEW - the DQ rule demo table, 7 rows across the column types */
287data dcdemo.mpe_x_new;
288 length some_char $32767 some_dropdown $128 some_hardselect $128
289 readonly_col $200 hidden_col $200 regex_hard_col $128
290 regex_soft_col $128 regex_both_col $128;
291 attrib some_char label='Some Character Column'
292 some_date label='Some Date' format=date9.
293 some_datetime label='Some Datetime' format=datetime19.
294 some_time label='Some Time' format=time8.
295 some_shortnum length=4
296 some_bestnum format=best.;
297 do primary_key_field=0 to 6;
298 some_char='this is dummy data';
299 some_dropdown='Option 1';
300 some_hardselect='Alpha';
301 some_num=0.00105564761956*(primary_key_field+1);
302 some_date=42+primary_key_field;
303 some_datetime=42+primary_key_field;
304 some_time=42+primary_key_field;
305 some_shortnum=8;
306 some_bestnum=44;
307 readonly_col='Readonly default';
308 hidden_col='Hidden default';
309 round_col=1.142857;
310 numfmt_col=1000*primary_key_field;
311 regex_hard_col='user@example.com';
312 regex_soft_col='SW1A 1AA';
313 regex_both_col='AB12 3CD';
314 output;
315 end;
316run;
317
318%mp_abort(iftrue= (&syscc ne 0)
319 ,mac=&_program
320 ,msg=%str(Problem creating the DC_DEMO tables in &demo_path)
321)
322
323/* register the tables so the EDIT and VIEW screens can find them */
324data append;
325 if 0 then set &lib..mpe_tables;
326 tx_from=0;
327 tx_to=253717919999;
328 libref='DC_DEMO';
329 loadtype='UPDATE';
330 num_of_approvals_required=1;
331 dsn='STUDENTS'; buskey='ID';
332 notes='Sample data - one row per student'; output;
333 dsn='VEHICLES'; buskey='MODEL';
334 notes='Sample data - a list of values for filters and dropdowns'; output;
335 dsn='FORMATS'; buskey='PK';
336 notes='Sample data - the date formats the viewer renders'; output;
337 dsn='MPE_X_SEARCH'; buskey='PRIMARY_KEY_FIELD';
338 notes='Full table search demo table'; output;
339 dsn='MPE_X_WIDE'; buskey='ROW_ID CHAR1 NUM1';
340 notes='Wide table fixture for the EDIT cell limit'; output;
341 dsn='MPE_X_NEW'; buskey='PRIMARY_KEY_FIELD';
342 notes='DQ rule demo table'; output;
343 dsn='MPE_X_FORMULA_TEST'; buskey='PRIMARY_KEY_FIELD';
344 notes='Formula demo table'; output;
345 dsn='MPE_X_FORMULA_PK_TEST'; buskey='PK_HARDFORMULA_COL PK_SOFTFORMULA_COL';
346 notes='Formula PK demo table'; output;
347 dsn='DEMO_01'; buskey='ID';
348 notes='Compact rule demo table (eight columns)'; output;
349 dsn='DERIVED_DEMO'; buskey='ID';
350 notes='Derived variable demo table'; output;
351 dsn='DEMO_ORDERS'; buskey='ORDER_ID';
352 notes='Hook demo - the real table the mirror points at';
353 output;
354 dsn='DEMO_MIRROR'; buskey='ORDER_ID';
355 notes='Hook demo - an empty mirror beside DEMO_ORDERS';
356 output;
357run;
358proc append base=&lib..mpe_tables data=append;
359run;
360
361%mp_abort(iftrue= (&syscc ne 0)
362 ,mac=&_program
363 ,msg=%str(Problem registering the DC_DEMO tables in &lib..mpe_tables)
364)
365
366/* leave no libref behind - makedata writes the assignment into settings */
367libname dcdemo clear;
368
369%mend mpe_makesampledata;