SAS,例如,在运行/修复结果后如何停止DO循环

发布于 2025-02-09 19:57:37 字数 493 浏览 1 评论 0原文

我目前正在尝试使用宏语言在EG中循环融合功能。我遇到的问题是循环填充了所有空列,而不仅仅是第一个空单元格。我尝试过一种逻辑形式来停止循环,但并未成功,并且通过比较相邻的列是否相同然后删除后,也尝试过解决结果。我希望循环仅填充第一个空单元格,因为之后我需要使用第二个变量运行第二个煤层循环。附件的图像显示了我得到的结果以及我的目标。

我当前的工作代码如下:

%macro coal;
data want;
set have;
%do i = 1 %to 5;
coal&i = coalescec(&column&i, var1);
%end;
run;
%mend;

​.sstatic.net/sfdsa.png" alt="output wanted">

I am currently trying to loop a coalesce function in EG using macro language. The issue I'm having is the loop is populating all empty columns, rather than just the first empty cell. I have tried a form of if logic to stop the loop but not successfully and also tried and failed to fix the results after by comparing if the adjacent columns are the same and then deleting. I would like the loop to only populate the first empty cell as I need to run a 2nd coalesce loop with a 2nd variable afterwards. The attached image shows what results I am getting and what I am aiming for.

My current working code is as follows:

%macro coal;
data want;
set have;
%do i = 1 %to 5;
coal&i = coalescec(&column&i, var1);
%end;
run;
%mend;

current results

output wanted

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半夏半凉 2025-02-16 19:57:37

一个数组循环,找到第一个空列,退出循环,并用var1:填充该列:

data want ;
  set have ;
  array c{*} column: ;
  /* loop over array and find first empty column */
  cmiss = . ;
  do i = 1 to dim(c) until (cmiss) ;
    if missing(c{i}) then cmiss = i ;
  end ;
  if cmiss then c{cmiss} = var1 ;
  drop i ;
run ;

An array loop, to find the first empty column, exit the loop, and populate that column with var1 :

data want ;
  set have ;
  array c{*} column: ;
  /* loop over array and find first empty column */
  cmiss = . ;
  do i = 1 to dim(c) until (cmiss) ;
    if missing(c{i}) then cmiss = i ;
  end ;
  if cmiss then c{cmiss} = var1 ;
  drop i ;
run ;
So要识趣 2025-02-16 19:57:37

您可以在数据步骤中完全执行此操作。如果要不断更新数据集并将其一个一个一个填充值填充,则可以使用数组。

示例数据:

data have;
    column1 = 123;
    column2 = 456;
    call missing(of column3-column5);
run;

代码:

data have;
    set have;
    array column[*] column:;

    /* Count non-missing columns */
    do i = 1 to dim(column);
        if(NOT missing(column[i]) ) then nonmiss+1;
            else leave;
    end;

    if(nonmiss < dim(column) ) then offset+1;

    do i = 1 to dim(column);
        if(missing(column[i]) ) then do;
            column[i] = column[offset];
            leave;
        end;
    end;

    drop i nonmiss;
run;

第一步:

column1 column2 column3 column4 column5 offset
123     456     123     .      .        1

第二步:

column1 column2 column3 column4 column5 offset
123     456     123     456     .       2

请注意,如果您再次运行此操作,它将从column3将值拉入column5

You can do this entirely within a data step. If you want to constantly update the dataset and have it fill in values one by one, you can use arrays.

Example data:

data have;
    column1 = 123;
    column2 = 456;
    call missing(of column3-column5);
run;

Code:

data have;
    set have;
    array column[*] column:;

    /* Count non-missing columns */
    do i = 1 to dim(column);
        if(NOT missing(column[i]) ) then nonmiss+1;
            else leave;
    end;

    if(nonmiss < dim(column) ) then offset+1;

    do i = 1 to dim(column);
        if(missing(column[i]) ) then do;
            column[i] = column[offset];
            leave;
        end;
    end;

    drop i nonmiss;
run;

First step:

column1 column2 column3 column4 column5 offset
123     456     123     .      .        1

Second step:

column1 column2 column3 column4 column5 offset
123     456     123     456     .       2

Note that if you run this again it will pull the value from column3 into column5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文