我必须从JSON样式的数据集创建一个文件,但每个变量之间没有CR。 所有变量必须在同一行。
我想要这样的东西:
ID1 "key1"="value1" "key2"="value2" .....
每个键是数据集的一列。 我在UNIX上使用此SAS 9.3。
样品: 我有
ID Name Sex Age
123 jerome M 30
345 william M 26
456 ingrid F 25`
我想要
123 "Name"="jerome" "sex"="M" "age"="30"
345 "Name"="william" "sex"="M" "age"="26"
456 "Name"="ingrid" "sex"="F" "age"="25"
谢谢
答案 0 :(得分:0)
如果您的数据看起来像这样...
Obs Name _NAME_ COL1
1 Alfred Name Alfred
2 Alfred Sex M
3 Alfred Age 14
4 Alfred Height 69
5 Alfred Weight 112.5
6 Alice Name Alice
7 Alice Sex F
8 Alice Age 13
9 Alice Height 56.5
10 Alice Weight 84
11 Barbara Name Barbara
12 Barbara Sex F
13 Barbara Age 13
14 Barbara Height 65.3
15 Barbara Weight 98
16 Carol Name Carol
17 Carol Sex F
18 Carol Age 14
19 Carol Height 62.8
20 Carol Weight 102.5
21 Henry Name Henry
22 Henry Sex M
23 Henry Age 14
24 Henry Height 63.5
25 Henry Weight 102.5
您可以使用类似这样的代码来编写值对。假设这就是您在说的。
189 data _null_;
190 do until(last.name);
191 set class;
192 by name;
193 col1 = left(col1);
194 if first.name then put name @;
195 put _name_:$quote. +(-1) '=' col1:$quote. @;
196 end;
197 put;
198 run;
Alfred "Name"="Alfred" "Sex"="M" "Age"="14" "Height"="69" "Weight"="112.5"
Alice "Name"="Alice" "Sex"="F" "Age"="13" "Height"="56.5" "Weight"="84"
Barbara "Name"="Barbara" "Sex"="F" "Age"="13" "Height"="65.3" "Weight"="98"
Carol "Name"="Carol" "Sex"="F" "Age"="14" "Height"="62.8" "Weight"="102.5"
Henry "Name"="Henry" "Sex"="M" "Age"="14" "Height"="63.5" "Weight"="102.5"
NOTE: There were 25 observations read from the data set WORK.CLASS.
答案 1 :(得分:0)
考虑这些非转置变体:
实际JSON,请使用Proc JSON
data have;input
ID Name $ Sex $ Age; datalines;
123 jerome M 30
345 william M 26
456 ingrid F 25
run;
filename out temp;
proc json out=out;
export have;
run;
* What hath been wrought ?;
data _null_; infile out; input; put _infile_; run;
----- LOG -----
{"SASJSONExport":"1.0","SASTableData+HAVE":[{"ID":123,"Name":"jerome","Sex":"M","Age":30},{"ID":345,"Name":"william","Sex":"M","Age":26},{"ID":456,"Name":"ingrid","Sex":"F","Age":25}]}
使用_ALL_
,使用PUT语句规范语法( variable-list )( format-list )对变量进行简明的名称/值对输出表示变量列表,=
表示格式。
filename out2 temp;
data _null_;
set have;
file out2;
put (_all_) (=);
run;
data _null_;
infile out2; input; put _infile_;
run;
----- LOG -----
ID=123 Name=jerome Sex=M Age=30
ID=345 Name=william Sex=M Age=26
ID=456 Name=ingrid Sex=F Age=25
使用VNEXT
例程迭代变量。使用VVALUEX
函数提取格式化的值,并有条件地构造引用的名称和值部分。
filename out3 temp;
data _null_;
set have;
file out3;
length _name_ $34 _value_ $32000;
do _n_ = 1 by 1;
call vnext(_name_);
if _name_ = "_name_" then leave;
if _n_ = 1
then _value_ = strip(vvaluex(_name_));
else _value_ = quote(strip(vvaluex(_name_)));
_name_ = quote(trim(_name_));
if _n_ = 1
then put _value_ @;
else put _name_ +(-1) '=' _value_ @;
end;
put;
run;
data _null_;
infile out3; input; put _infile_;
run;
----- LOG -----
123 "Name"="jerome" "Sex"="M" "Age"="30"
345 "Name"="william" "Sex"="M" "Age"="26"
456 "Name"="ingrid" "Sex"="F" "Age"="25"