全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1297 2
2020-12-29
做sas base 模考时遇到一道题 ,题目如下:

·        Step 1:

o  Create a temporary data set, cleandata36.

o  In this data set, convert all groupvalue to upper case.

o  Then keep only observationswith group equal to ‘A’ or ‘B’.

·        Step 2:

o  Determine the MEDIAN value forthe Kilograms variable for each group (A,B) in the cleandata36data set. Round MEDIAN to the nearest whole number.

·        Step 3:

o  Create results.output36from cleandata36.

o  Ensure that all values forvariable Kilograms are between 40 and 200, inclusively.

o   If the value is missing or out of range, replace the value with theMEDIAN Kilograms value for the respective group (A,B) calculatedin step 2.

Run the program and use the results to answer the next 3 questions.

三个问题分别是:How many observations are in results.output36?What is the MEAN Kilograms value for group=’A’ in the results.output36 data set?  What is the MEAN Kilograms value for group=’B’ in the results.output36data set?


我第一个问题答案是4897,回答错了,正确答案是4992。但是后两个问题都回答正确。对比答案发现,问题可能是出现在step1的赋值上。

我的代码如下:

data cleandata360;

set cert.input36;

group=upcase(group);

where group in ("A" "B");  /*官方答案没有对group转化为大写,而是直接 if upcase(group) in ('A','B');其它步骤一样*/

run;


另外看到一种答案也得出4992的结果,代码如下:

data cleandata36a (drop =group);

set cert.input36;

group1=upcase(group);

run;


data new123;

set cleandata36a;

where group1 in ('A' 'B');

rename group1=group;


run;



求问 我的写法为什么会少观测值呢?先谢谢各位大佬了







二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2020-12-30 09:35:10
这是因为where和if的区别所导致的,简单来讲,where在进PDV之前执行筛选,if在进入PDV之后执行筛选。具体可以搜索一下where与if的区别。 你的代码里如果把where换成if,出来的观测数就应该是对的了。 供参考~
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2020-12-30 21:09:15
洪哥得势1 发表于 2020-12-30 09:35
这是因为where和if的区别所导致的,简单来讲,where在进PDV之前执行筛选,if在进入PDV之后执行筛选。具体可 ...
谢谢回复!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群