使用 SAS datastep 创建 "pair key=value" 文件
create "pair key=value" file with SAS datastep
我必须从 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"
谢谢
如果您的数据是这样的...
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.
考虑这些非移调变体:
实际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}]}
使用 PUT 语句规范语法 (variable-list) (format-list 的变量的简明名称-值对输出),使用 _ALL_
作为变量列表,使用 =
作为格式。
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_ _value_ 000;
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"
我必须从 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"
谢谢
如果您的数据是这样的...
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.
考虑这些非移调变体:
实际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}]}
使用 PUT 语句规范语法 (variable-list) (format-list 的变量的简明名称-值对输出),使用 _ALL_
作为变量列表,使用 =
作为格式。
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_ _value_ 000;
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"