在 SAS 中使用变量名进行计算 [英] Use variable name for calculation in SAS

查看:35
本文介绍了在 SAS 中使用变量名进行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的 SAS 数据集:

I have a SAS dataset that looks like this:

var  _12  _41  _17
12      .   .   .
41      .   .   .
17      .   .   .

因此对于每个 var 都有一个名为 _var 的列.

So for each var there is a column named _var.

我想使用数组或宏用行和列的乘积填充所有缺失值:

I want to use an array or macro to populate all the missing values with the product of the row and column:

     _12  _41  _17
12  144   492  204
41  492  1681  697
17  204   697  289

关于如何解决这个问题的任何想法?我希望它是完全通用的,所以我不需要知道列的名称,并且不对它们的顺序或值做任何假设,除了它们都是数字.

Any thoughts on how to approach this? I want it to be completely general, so I don't need to know the names of the columns, and make no assumptions about their order or values, other than that they are all numbers.

推荐答案

由于所有变量(除 var 外)都以下划线开头,因此很容易在数组中引用它们.然后,您可以使用 INPUT、COMPRESS 和 VNAME 函数提取数字并在一行中执行计算!这是代码.

As all the variables (apart from var) begin with an underscore then it is easy to reference them in an array. You can then use the INPUT, COMPRESS and VNAME functions to extract the number and perform the calculation in a single line! Here is the code.

    data have;
    input var  _12  _41  _17;
    cards;
    12  .   .   .
    41  .   .   .
    17  .   .   .
    ;
    run;

    data want;
    set have;
    array nums{*} _: ;
    do i=1 to dim(nums);
        nums{i}=var*input(compress(vname(nums{i}),"_"),best12.);
    end;
    drop i;
    run;

这篇关于在 SAS 中使用变量名进行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆