DATA Step, Macro, Functions and more

Array error message

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 138
Accepted Solution

Array error message

Hello,

Basically, I am trying to see if Variable X, for which I have monthly values, changes over time for participants. I have created a dataset where for each subject their values for Variable X are printed for each month in the time period. Unfortunately, sometimes Variable X is missing for a subject.

I tried to insert an array as follows:

data mydata;

array varx value1--value60;

do i=1-70;

if varx(i)=. then varx(i)=varx(i-1);

end;

run;

Unfortunately this creates two problems. One is what to do if participants' first value for Variable X is missing. The second is that I keep getting an error message that all variables in array list must be the same type. Considering that all the variables in the array list are numeric, I'm not sure why I'm getting this error message or how to fix it.

Any assistance is much appreciated.


Accepted Solutions
Solution
‎01-09-2013 10:53 AM
PROC Star
Posts: 7,480

Re: Array error message

Posted in reply to Walternate

largest(1,of vSmiley Happy returns the largest value of any variable that begins with the letter v.  If they are all missing it returns a missing value.

smallest(1,of vSmiley Happy returns the smallest value of any variable that begins with the letter v.  If they are all missing it returns a missing value.

ifn compare the results of the two functions.  If both functions return the same value (regardless of whether the value is a number or a missing value), flag gets set to 0.  If they return different values, flag gets set to 1.

View solution in original post


All Replies
Respected Advisor
Posts: 4,927

Re: Array error message

Posted in reply to Walternate

Try this (note the single dash between value1 and value60) :

data mydata;

array varx value1-value60;

if missing(value(1)) then value1 = 0; /* Replace 0 with whatever is appropriate */

do i = 2 to 60;

     if missing(varx(i)) then varx(i)=varx(i-1);

     end;

run;

PG

PG
Trusted Advisor
Posts: 2,116

Re: Array error message

Posted in reply to Walternate

Note that your do loop goes to 70, but you only have 60 elements in the array.

The LOF (Last Observation Forward) approach to data inputation has a long, mostly, negative history.  It basically assumes the response surface is flat, and that the assumption that you are trying to examine.

PROC Star
Posts: 7,480

Re: Array error message

Posted in reply to Walternate

I'll point out some additional problems you have with the code you posted:

1. You are attempting to load the array with the -- operator when, in fact, you only want the - operator.  With -- you are including all variables between value1 and value60, while with the - operator you are only incuding those that begin with the prefix 'value' and end with a number between 1 and 60.

2. Your datastep doesn't include a set statement, thus doesn't bring in ANY data.

3. You specify your loop as 1-70.  Like Doc mentioned, it should be 60 rather than 70 but, additionally, should be defined as 1 to 60

4,  If you are only trying to identify if there is a difference between the values, not populating the missing values, why not approach the task a bit differently.  e.g.:

data mydata_want;

  set mydata_have;

  same=ifc(max(of value1-value60)*

          (60-nmiss(of value1-value60))=

          sum(of value1-value60),'NoChange','Change');

run;

Frequent Contributor
Posts: 138

Re: Array error message

Thank you for your response. Could you explain what the code you provided will do? Thanks!

PROC Star
Posts: 7,480

Re: Array error message

Posted in reply to Walternate

The code creates a variable called "same".  It ignores missing values but, for any remaining values, if at least one is different from the others, it assigns a value of "change" to the variable same.  If all values are the same, it assigns a value of "same" to the variable same.

Super Contributor
Posts: 1,636

Re: Array error message

Posted in reply to Walternate

Hi,

How about the code below:

data one;

input v1-v5;

cards;

20 . 30 35 15

10 80 . . 20

20 . 20 20 20

88 88 88 88 88

18 18 18 18 18

20 15 10 8 6

;

data two;

  set one;

  flag=ifn(largest(1,of vSmiley Happy=largest(5-nmiss(of vSmiley Happy,of vSmiley Happy,0,1);

proc print;run;

                           Obs    v1    v2    v3    v4    v5    flag

                             1     20     .    30    35    15      1

                             2     10    80     .     .    20      1

                             3     20     .    20    20    20      0

                             4     88    88    88    88    88      0

                             5     18    18    18    18    18      0

                             6     20    15    10     8     6      1

PROC Star
Posts: 7,480

Re: Array error message

: Actually, I like a variant of your suggested code much better than the one I suggested.  How about:

data one;

  input v1-v5;

  cards;

20 . 30 35 15

10 80 . . 20

20 . 20 20 20

88 88 88 88 88

18 18 18 18 18

. . . . .

. 1 . . .

20 15 10 8 6

;

data two;

  set one;

  flag=ifn(largest(1,of vSmiley Happy=smallest(1,of vSmiley Happy,0,1);

run;

Super Contributor
Posts: 1,636

Re: Array error message

Better!Smiley Happy

Frequent Contributor
Posts: 138

Re: Array error message

That worked! Could you please explain what the coding means?

Super Contributor
Posts: 1,636

Re: Array error message

Posted in reply to Walternate

Which one worked?

Frequent Contributor
Posts: 138

Re: Array error message

Sorry, the most recent one from Mr. Tabachneck.

Solution
‎01-09-2013 10:53 AM
PROC Star
Posts: 7,480

Re: Array error message

Posted in reply to Walternate

largest(1,of vSmiley Happy returns the largest value of any variable that begins with the letter v.  If they are all missing it returns a missing value.

smallest(1,of vSmiley Happy returns the smallest value of any variable that begins with the letter v.  If they are all missing it returns a missing value.

ifn compare the results of the two functions.  If both functions return the same value (regardless of whether the value is a number or a missing value), flag gets set to 0.  If they return different values, flag gets set to 1.

Super Contributor
Posts: 1,636

Re: Array error message

Hi Art,

An English question. is "set" a verb or something else in your "If they return different values, flag gets set to 1."?

Super User
Super User
Posts: 7,060

Re: Array error message

It is a verb in that sentence.  More of a computer programming usage than normal English.  A similar computer programming verb would be ASSIGN. The closest normal English usage for SET as a verb would in something like.   " I'm cold, could you please set the thermostat to a higher temperature? "

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 14 replies
  • 381 views
  • 1 like
  • 6 in conversation