首页 > 解决方案 > 问题:引用数组值,但返回零

问题描述

我一直在研究随机选择数组中的项目。下面,我概述了我的过程。我已成功完成第 6 步(进行了许多数据检查),但由于某种原因,当我引用数组时,我收到一个零值。这一直令人困惑,因为即使我检查原始排序数据并注意某个值,检索到的值也是零。此外,我运行了一个 VNAME 来查看它正在提取哪个变量,并且它对应于数组中的正确位置。有谁知道为什么我从数组中返回一个零值?

*STEP 1: Set all non-codes to zero;
ARRAY CEREAL [337] ha_DTQ02_1-ha_DTQ02_337;
DO i=1 to 337;
if CEREAL[i]=88888.00  THEN CEREAL[i]=0;
END;

*STEP 2: Sort so that all zero values come first and food codes come last;
call SORTN(ha_DTQ02_1-ha_DTQ02_337);

*STEP 3: Rename array in reverse order so that zeros come last and codes are first. Sort function above only works in ascending order;
RENAME  ha_DTQ02_1- ha_DTQ02_337=ha_DTQ02_337-ha_DTQ02_1;

*STEP 4: Count number of cereals selected;  
ARRAY CEREALS[337]ha_DTQ02_1-ha_DTQ02_337;
NUMCEREALS=0;
DO i=1 to 337;
IF CEREALS[i] NOT IN (.,0) THEN NUMCEREALS+1;
END;

*STEP 5: get a random number between those two numbers- this works just fine;
IF  NUMCEREALS NE 0 THEN rand1 = rand('integer', 1, numCereals);

*ensure that your second random number isn't the same as the first random number;
if NUMCEREALS ge 2 then do until(rand2 ne rand1);
    rand2 = rand('integer', 1, numCereals);

end;

*STEP 6: Pull value from array using random number.;
Note: This is where I am stuck. I have tried alternative code where I recreated a new array and tried to pull the values from that new array. I have also tried placing the code directly below before closing the do loop. When the code does run, the value for these variables is zero. After many data checks, steps 1-5 work well and achieve their goals. 
dtd020Af = CEREALS (rand1);
dtd020Bf = CEREALS (rand2);

OPTIONS NOFMTERR;
run;

标签: arrayssas

解决方案


SORTN呼叫例程需要操作员才能使用OF名单。

call SORTN(of ha_DTQ02_1-ha_DTQ02_337);

LOG窗户的敏锐眼睛应该向您显示警告

3214    call SORTN(ha_DTQ02_1-ha_DTQ02_337);
             -----
             134
WARNING 134-185: Argument #1 is an expression, which cannot be updated by the SORTN subroutine
                 call.


推荐阅读