Temporary arrays

Solved
Occasional Contributor
Posts: 12

Temporary arrays

Please help!  I need to create 5 new variables Pass1 ~ Pass5 by using ARRAY statement to determine whether variables Score1 ~ Score5 are greater than or equal to 65, 70, 60, 62, 68, respectively. If yes, the new variable is equal to 1; otherwise, it is equal to 0.

I would appreciate it.

Here is my program:

data psych;

input ID \$3. Ques1-Ques10 Score1-Score5;

ARRAY RQues (10)RQues1-RQues10;

ARRAY Ques (10) Ques1-Ques10;

DO i = 1 to 10;

RQues (   i  )  =  6 - Ques (i);

END;

ToTQues = SUM(OF Ques1 -Ques10);

Missing = NMISS(OF Ques1 - Ques10);

datalines;

001 1 3 2 4 5 4 3 4 5 4 90 88 92 95 90

002 3 3 . . 3 4 5 5 1 . 64 64 77 72 71

003 . . . . 5 5 4 4 3 3 68 69 80 75 70

004 5 3 4 5 . 5 4 3 3 . 88 77 66 77 67

005 5 4 3 2 1 1 2 3 4 5 92 93 94 95 99

006 4 5 1 3 2 5 3 1 1 1 55 65 71 85 91

007 3 2 2 1 5 4 5 3 2 2 60 70 88 79 82

008 2 1 1 5 5 4 4 2 1 3 65 87 90 60 55

009 1 1 1 3 2 2 3 5 4 4 71 81 91 77 88

010 2 3 3 4 1 5 2 2 3 1 64 69 61 70 70

;

proc print data = psych;

run;

Accepted Solutions
Solution
‎06-27-2015 05:18 PM
Super Contributor
Posts: 326

Re: Temporary arrays

If your aim is to get the new Variables (Pass1-Pass5) from Score1-Score5, then your program is not helping you.

If I understand your requirements correct, the following program will answer you. If it doesn't, get back with the required

output and someone direct you in the right direction.

data psych(drop=i);

input ID \$3. Ques1-Ques10 Score1-Score5;

array S

• Score1 - Score5;
•   array F[5] _temporary_ (65, 70, 60, 62, 68);

array P

• Pass1 - Pass5;
•   do i = 1 to dim(S);

P = (S >= F);

end;

output;

datalines;

001 1 3 2 4 5 4 3 4 5 4 90 88 92 95 90

002 3 3 . . 3 4 5 5 1 . 64 64 77 72 71

003 . . . . 5 5 4 4 3 3 68 69 80 75 70

004 5 3 4 5 . 5 4 3 3 . 88 77 66 77 67

005 5 4 3 2 1 1 2 3 4 5 92 93 94 95 99

006 4 5 1 3 2 5 3 1 1 1 55 65 71 85 91

007 3 2 2 1 5 4 5 3 2 2 60 70 88 79 82

008 2 1 1 5 5 4 4 2 1 3 65 87 90 60 55

009 1 1 1 3 2 2 3 5 4 4 71 81 91 77 88

010 2 3 3 4 1 5 2 2 3 1 64 69 61 70 70

;

run;

proc print data = psych;

run;

All Replies
Posts: 1,270

Re: Temporary arrays

data psych;

input ID \$3. Ques1-Ques10 Score1-Score5;

ARRAY pass (5) pass1-pass5 (5*0);

ARRAY score (5) Score1-Score5;

DO i = 1 to dim(score);

if score{i}>=60 then pass{i}=1;

END;

datalines;

001 1 3 2 4 5 4 3 4 5 4 90 88 92 95 90

002 3 3 . . 3 4 5 5 1 . 64 64 77 72 71

003 . . . . 5 5 4 4 3 3 68 69 80 75 70

004 5 3 4 5 . 5 4 3 3 . 88 77 66 77 67

005 5 4 3 2 1 1 2 3 4 5 92 93 94 95 99

006 4 5 1 3 2 5 3 1 1 1 55 65 71 85 91

007 3 2 2 1 5 4 5 3 2 2 60 70 88 79 82

008 2 1 1 5 5 4 4 2 1 3 65 87 90 60 55

009 1 1 1 3 2 2 3 5 4 4 71 81 91 77 88

010 2 3 3 4 1 5 2 2 3 1 64 69 61 70 70

;

Solution
‎06-27-2015 05:18 PM
Super Contributor
Posts: 326

Re: Temporary arrays

If your aim is to get the new Variables (Pass1-Pass5) from Score1-Score5, then your program is not helping you.

If I understand your requirements correct, the following program will answer you. If it doesn't, get back with the required

output and someone direct you in the right direction.

data psych(drop=i);

input ID \$3. Ques1-Ques10 Score1-Score5;

array S

• Score1 - Score5;
•   array F[5] _temporary_ (65, 70, 60, 62, 68);

array P

• Pass1 - Pass5;
•   do i = 1 to dim(S);

P = (S >= F);

end;

output;

datalines;

001 1 3 2 4 5 4 3 4 5 4 90 88 92 95 90

002 3 3 . . 3 4 5 5 1 . 64 64 77 72 71

003 . . . . 5 5 4 4 3 3 68 69 80 75 70

004 5 3 4 5 . 5 4 3 3 . 88 77 66 77 67

005 5 4 3 2 1 1 2 3 4 5 92 93 94 95 99

006 4 5 1 3 2 5 3 1 1 1 55 65 71 85 91

007 3 2 2 1 5 4 5 3 2 2 60 70 88 79 82

008 2 1 1 5 5 4 4 2 1 3 65 87 90 60 55

009 1 1 1 3 2 2 3 5 4 4 71 81 91 77 88

010 2 3 3 4 1 5 2 2 3 1 64 69 61 70 70

;

run;

proc print data = psych;

run;

Occasional Contributor
Posts: 12

Re: Temporary arrays

datasp:

Thank you so much!!! The program did work, I got the new variables with the correct values.  Could you please explain what part of the program specified that Pass= 1 while not Pass =0?  I do not see any IF or ELSE statements.

Thank you,

Super Contributor
Posts: 326

Re: Temporary arrays

In the program,

data psych(drop=i);

input ID \$3. Ques1-Ques10 Score1-Score5;

array S

• Score1 - Score5;
•   array F[5] _temporary_ (65, 70, 60, 62, 68);

array P

• Pass1 - Pass5;
•   do i = 1 to dim(S);

P = (S >= F);

end;

output;

The statement:

P = (S >= F);

works as follows:

if S >= F then P = 1;

else P = 0;

The compact form, (S >= F), computes a logical variable taking value 1 or 0 depending on True or False respectively based on the comparison.

Occasional Contributor
Posts: 12

Re: Temporary arrays

Definitively. I overlooked that.  Thank you so much again.

Occasional Contributor
Posts: 12

Re: Temporary arrays

Thank you all for your help.

🔒 This topic is solved and locked.

Discussion stats
• 6 replies
• 287 views
• 3 likes
• 3 in conversation