在 SAS 中使用 JavaScript + D3 3.x 代码创建 HTML 输出



我在这里执行代码时面临着挑战:在 SAS EG 上 https://jsfiddle.net/zwk29rcs/1/。链接中的代码使用 JavaScript 和 D3 3.x 创建桑基图,并希望使用 SAS EG 为内部数据创建相同的图。

我尝试使用PROC STREAM和PROC模板,但它生成了空白的HTML输出。

如果您能指导我完成这个项目,将不胜感激。

谢谢!

输入数据 jsfiddle 的代码:

var data = [{
"source": "A",
"target": "X",
"value": 5
}, {
"source": "A",
"target": "Y",
"value": 7
}, {
"source": "A",
"target": "Z",
"value": 6
}, {
"source": "B",
"target": "X",
"value": 2
}, {
"source": "B",
"target": "Y",
"value": 9
}, {
"source": "B",
"target": "Z",
"value": 4
}, {
"source": "X",
"target": "L",
"value": 5
}
];

总是有蛮力方法,只需直接写入HTML文件即可。

使用 PUT 语句推送直接文本,您还可以有条件地只在何时放置某些语句。

下面是一个未经测试的粗略示例:

data _null_;
file 'demo.html'; *name and properties of file to be created;
set source_data; *input data set;
if _n_ =1 then do; *put all beginning text before data;
put '<!doctype html>';
put '<html>';
put '<head>';
....
put ' var data = [';
end;
*******************************************************;
*create data portion;
*******************************************************;
put    '{"source": "' source_variable '",';
put    '"target": "' x_variable '",';
put    '"value": ' value_variable;
put    '}';
*only place comma between elements so last one does not need one;
if not eof then do;
put ',';
end;
*place all lines of code after;
if eof then do;
put '];';
*rest of code;
put ' graph = {
"nodes": [],
"links": []
};';
end;
run;

你真的需要它以那种难以阅读的风格格式化吗?

无论如何,使用数据步骤很容易生成它。

由于您没有显示您尝试传递的 SAS 中拥有哪些数据,让我们创建一个看起来像您发布的 JavaScript 代码中的数据的数据集,以便我们有一些东西可以编程。

data have ;
input source $ target $ value ;
cards;
A X 5
A Y 7
A Z 6
B X 2
B Y 9
B Z 4
X L 5
;

这是一个数据步骤代码,用于将数据生成为您需要的类似 json 的样式。

filename test temp;
data _null_;
file test;
set have end=eof;
format _character_ $quote. ;
if _n_=1 then put 'var data =' / '[' @;
else put ',' @;
put '{"source": ' source ',"target": ' target ',"value": ' value '}' ;
if eof then put '];';
run;

结果:

var data =
[{"source": "A" ,"target": "X" ,"value": 5 }
,{"source": "A" ,"target": "Y" ,"value": 7 }
,{"source": "A" ,"target": "Z" ,"value": 6 }
,{"source": "B" ,"target": "X" ,"value": 2 }
,{"source": "B" ,"target": "Y" ,"value": 9 }
,{"source": "B" ,"target": "Z" ,"value": 4 }
,{"source": "X" ,"target": "L" ,"value": 5 }
];

将此输出定向到您需要的位置应该不难。 概括输出任何数据集的方法也不是那么难,特别是如果数据集变量名称与 json 字段所需的名称匹配。

最新更新