用另一个现有数据集的值替换一个现有数据集的值。

huangapple go评论53阅读模式
英文:

Replace values from one existing dataset with values from another existing dataset

问题

我有 dataset1,想要用 dataset2 中相同变量的值替换 variableX 的当前值。我希望 dataset1 的其余部分保持不变。

如何做到这一点?

另外,dataset1 有 89 个观察值,而 dataset2 有 9098 个观察值。我只需要从 dataset2 中取前 89 个观察值。

谢谢

我尝试了以下方法:

libname data '\\filepath\dataset1folder';
libname data2 '\\filepath\dataset2folder';

data data.dataset1;
	set data2.dataset2 (obs=89);
	CWEIGHT = CWEIGHT;
RUN;
英文:

I have dataset1 that I want to replace the current values for variableX with the values for that same variable from dataset2. I want the rest of dataset1 to remain the same.

How do I do this?

Also, dataset1 has 89 observations and dataset2 has 9098 observations. I only need the first 89 observations from dataset2.

thank you

I tried the following

libname data '\\filepath\dataset1folder';
libname data2 '\\filepath\dataset2folder';

data data.dataset1;
	set data2.dataset2;
	CWEIGHT = CWEIGHT;
RUN;

答案1

得分: 0

你的要求:

- 用dataset2中同一变量的值替换dataset1中variableX的当前值。我希望dataset1的其余部分保持不变。
- 我只需要从dataset2获取前89个观察值。

你需要获取dataset2中的前89个观察值,并且只需要variableX。可以使用`KEEP=`和`OBS=`来实现:

set data2.dataset2 (KEEP=variableX OBS=89);


然后,你希望使用这些数据更新dataset1,因此你需要使用一个数据步骤来读取和写入dataset1,并且同时加入上述条件:

libname data '\filepath\dataset1folder';
libname data2 '\filepath\dataset2folder';

data data.dataset1;
/* 数据步骤是一个循环 - 此语句加载dataset1的第一行 /
set data.dataset1;
/
这加载了dataset2的第一行,但只包括variableX - 如果variableX在dataset1中存在,它会被覆盖 */
set data2.dataset2 (KEEP=variableX OBS=89);
RUN;

英文:

Your requirements:

  • Replace the current values in dataset1 for variableX with the values for that same variable from dataset2. I want the rest of dataset1 to remain the same.
  • I only need the first 89 observations from dataset2.

You need to obtain the first 89 observations from dataset2 and only variableX. This can be achieved using KEEP= and OBS=:

set data2.dataset2 (KEEP=variableX OBS=89);

And you want to update dataset1 using this data, so you need to use a datastep that will read and write dataset1, while also incorporating the above:

libname data '\\filepath\dataset1folder';
libname data2 '\\filepath\dataset2folder';

data data.dataset1;
    /* The data step is a loop - this statement loads the first row of dataset1 */
    set data.dataset1;
    /* This loads the first row of dataset2, but only variableX - if variableX is present in dataset1 then it is overwritten */
    set data2.dataset2 (KEEP=variableX OBS=89);
RUN;

答案2

得分: 0

你可以使用merge语句和in=数据集选项。

data want;
  merge data.dataset1(in=_in1_) data2.dataset2(keep=variableX);
  if _in1_=1;
run;

想象这些数据集是盒子,merge语句只是将这些盒子水平放在一起,并对齐它们的顶部。如果不同数据集中存在相同名称的变量,则前一个将被后一个覆盖。
在数据集后面,in=选项会创建一个0-1变量,例如这个例子中的_in1_,以指示观察是否来自该数据集。由于你只想要来自dataset1的观察,你可以使用if _in1_=1来对合并结果进行子集筛选。

英文:

You can use the merge statement and in= dataset option.

data want;
  merge data.dataset1(in=_in1_) data2.dataset2(keep=variableX);
  if _in1_=1;
run;

Image the datasets are boxes, the merge statement just places these boxes next to each other horizontally and aligns their tops. If there are same name variables in different datasets, the former one will be rewrite by the following one.
Following a dataset, the in= option create a 0-1 variable, _in1_ in this example, to indicate if one observation comes from this dataset or not. Since you only want to observations from dataset1, you can use if _in1_=1 to subset the result of merging.

huangapple
  • 本文由 发表于 2023年5月11日 06:53:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定