嗨,我正在尝试将一个表拆分为三个表,只有一个数据步骤。当我向这些表输出行时,我需要创建一个名为diet_type的变量来显示饮食一、饮食二或饮食三。这是我对整个数据集的代码,但我认为最相关的代码是在第二个数据步骤之后。这是我的data i.stack.imgur.com/8gVka.png
导入和预处理
options VALIDVARNAME=V7;
proc import datafile="/home/u54324957/The Files/Diet.csv" out=Diet dbms=csv replace;
run;
data dietfile;
set Diet;
First_Name=PROPCASE(First_Name);
Last_Name=PROPCASE(Last_Name);
FullName=strip(Last_Name) || "," || strip(First_Name);
Length SexVariable $ 6;
if Sex=0 then SexVariable="Male";
else if Sex=1 then SexVariable="Female";
drop Sex;
rename SexVariable=Sex;
diet_num=input(substr(diet, 6, 1), 1.);
pre_weightlbs=pre_weight * 2.205;
format pre_weightlbs 5.1;
post_weightlbs=weight10weeks * 2.205;
format post_weightlbs 5.1;
weightloss=pre_weight - weight10weeks;
format weightloss 4.1;
drop Last_Name First_Name pre_weight Diet weight10weeks;
run;最相关的代码:拆分
data Diet1 Diet2 Diet3;
set Diet;
if diet_num = 1 then do;
diet_num = 1;
output Diet1;
end;
else if diet_num=2 then do;
diet_num = 2;
output Diet2;
end;
else do;
diet_num = 3;
output Diet3;
end;
run;根据日志,出于某种原因,只有Diet3有任何观察结果。谁能帮我把这张桌子分成三张桌子?
发布于 2021-02-01 00:33:51
请注意,您可能不需要拆分数据集,因为您始终可以只使用WHERE语句来根据DIET_NUM的值过滤单个数据集。
下面是应该直接读入文本文件并一次写出所有三个数据集的代码。我包含了第四个数据集,用于捕获DIET_NUM不是1、2或3的任何观察结果。
proc format ;
value sex 0='Male' 1='Female';
run;
data diet1 diet2 diet3 other ;
infile "/home/u54324957/The Files/Diet.csv" dsd truncover firstobs=2;
* Read in data, use in-line informats to give hint to compiler ;
* what length to use when creating the character variables ;
input First_Name :$30. Last_Name :$30. ID Sex Age Height PreWeight Diet_num Weight10weeks ;
* Define the variables that will be derived ;
length FullName $62 pre_weightlbs post_weightlbs weightloss 8;
* Clean up case on names and generate full name ;
First_Name=PROPCASE(First_Name);
Last_Name=PROPCASE(Last_Name);
FullName=catx(', ',Last_Name,First_Name);
* Convert to pounds and calculate change in weight ;
pre_weightlbs=pre_weight * 2.205;
post_weightlbs=weight10weeks * 2.205;
weightloss=pre_weight - weight10weeks;
* Attach formats to variables that need them. ;
format sex sex. pre_weightlbs post_weightlbs 5.1 weightloss 4.1;
* Split into multiple datasets ;
if diet_num=1 then output diet1;
else if diet_num=2 then output diet2;
else if diet_num=3 then output diet3;
else output other;
run;https://stackoverflow.com/questions/65975182
复制相似问题