将一个数据集的值除以另一个数据集的行数。

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

Divide values of a data set by nrows of another data set

问题

假设有以下表格:

 ID     Value 
 A       234
 B       231
 C       223
 D       167
 E       57

你知道另一个数据集的行数是"xxx",例如1,234。
现在,你想将每一行("Value"列)除以"xxx"。我不想明确显示"xxx"的值,因为它可能会根据数据集而变化。
期望的输出:

 ID     Value 
 A       0.189
 B       0.187
 C       0.180
 D       0.135
 E       0.046    

有人可以帮帮我吗?

英文:

suppose to have the following table:

> ID Value
> A 234
> B 231
> C 223
> D 167
> E 57

And you know that the number of rows of another data set is "xxx", for example 1,234.
Now, you want to divide each row ("Value" column) by the "xxx". I would like not to show explicitly the value of "xxx" because depending on the data set it could change.
Desired output:

> ID Value
> A 0.189
> B 0.187
> C 0.180
> D 0.135
> E 0.046

Can anyone help me please?

答案1

得分: 2

给定如下数据:

data have ;
  input ID $1. Value ;
  cards ; 
A 234
B 231
C 223
D 167
E 57
;

data other ;
  do id=1 to 1234 ;
    output ;
  end ;
run ;

你可以使用带有NOBS选项的SET语句来确定OTHER数据集中的记录数。

data want ;
  set have ;
  if 0 then set other (drop=id) nobs=nobs ;
  Value2=Value/nobs ;
run ;
英文:

Given data like:

data have ;
  input ID $1. Value ;
  cards ; 
A 234
B 231
C 223
D 167
E 57
;

data other ;
  do id=1 to 1234 ;
    output ;
  end ;
run ;

You can use the SET statement with NOBS option to determine the number of records in OTHER.

data want ;
  set have ;
  if 0 then set other (drop=id) nobs=nobs ;
  Value2=Value/nobs ;
run ;

答案2

得分: 1

你可以使用NOBS= SET语句选项来获取数据集中的观测数。

data value;
    input id:$1. value;
    cards;
    A       234
    B       231
    C       223
    D       167
    E       57
    ;;;;
run;
data temp;
    if 0 then set sashelp.class(drop=_all_) nobs=nobs;
    set value;
    d = nobs;
    x = value/d;
run;
proc print;
run;

将一个数据集的值除以另一个数据集的行数。

英文:

You can get the number of obs in a data set using the NOBS= SET statement option.

data value;
    input id:$1. value;
    cards;
    A       234
    B       231
    C       223
    D       167
    E       57
    ;;;;
run;
data temp;
    if 0 then set sashelp.class(drop=_all_) nobs=nobs;
    set value;
    d = nobs;
    x = value/d;
run;
proc print;
run;

将一个数据集的值除以另一个数据集的行数。

答案3

得分: 0

有很多不同的方法可以获取数据集的总行数,但最常见的方法是使用几个函数调用。SAS已经知道SAS数据集中的总行数,所以你不需要计算它们。我们将从数据集中提取该属性,将其保存到一个宏变量中,然后在数据步骤中将数据集的所有值除以该变量的值。

另一种方法是:

data _null_;
    set otherdata nobs=n;
    call symputx('nobs', n);
    stop;
run;

data want;
    set have;
    value = value/&nobs;
run;

你还可以创建一个宏,以便在任何时候调用时获取数据集的观测数。

%macro nobs(data);
    %let dsid = %sysfunc(open(&data));
    %let nobs = %sysfunc(attrn(&dsid, nlobs));
    %let rc   = %sysfunc(close(&dsid));

    &nobs
%mend;

现在,你可以在任何时候调用`%nobs`来获取数据集的总观测数。

```sas
data want;
    set have;
    value = value/%nobs(otherdata);
run;
英文:

There are a lot of different ways to get the total number of rows of a dataset, but the most common one is to use a few function calls. SAS already knows the total number of rows in a SAS dataset, so you do not need to count them. We'll pull that attribute from the dataset, save it to a macro variable, then divide all the values of your dataset in a data step.

%let dsid = %sysfunc(open(otherdata));     * Open the dataset and create a dataset id;
%let nobs = %sysfunc(attrn(&dsid, nlobs)); * Get the number of rows;
%let rc   = %sysfunc(close(&dsid));        * Close it;

data want;
    set have;
    value = value/&nobs;
run;

Alternatively:

data _null_;
    set otherdata nobs=n;
    call symputx('nobs', n);
    stop;
run;

data want;
    set have;
    value = value/&nobs;
run;

You also create a macro that you can call any time you want the number of observations of a dataset.

%macro nobs(data);
    %let dsid = %sysfunc(open(&data));
    %let nobs = %sysfunc(attrn(&dsid, nlobs));
    %let rc   = %sysfunc(close(&dsid));

    &nobs
%mend;

Now you can call %nobs whenever you want the total number of observations of a dataset.

data want;
    set have;
    value = value/%nobs(otherdata);
run;

huangapple
  • 本文由 发表于 2023年8月8日 22:38:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860606.html
匿名

发表评论

匿名网友

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

确定