Calcite | Level 5

## SAS array and transpose

I am using SAS University Edition on a Mac, I need help in how to to code per the questions after the dataset:

SAS Dataset

data psych;

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

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

;

runs;

In one data step --

• Create 10 new variables RQues1 ~ RQues10 by using ARRAY statement to reverse the values (e.g., 1 to 5, 2 to 4, etc.) of Ques1 to Ques10. Notice that original missing data are still missing in new variables.
• Create a new variable ToTQues by using SUM function to make a summation number from variables Ques1 to Ques10.
• Create a new variable Missing to count how many variables from Ques1 to Ques10 with missing data in each ID. (Hint: there is SAS function to count variables with missing data).
• 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. Notice that (65, 70, 60, 62, 68) should be defined in a temporary array.
• Rearrange the position of variables in the final data set by the following order:

ID Ques1 . . . Ques10 RQues1 . . . RQues10 ToTQues Missing Score1 . . . Score5

• Print all variables in the output window and copy to below.

4 REPLIES 4
Super User

## Re: SAS array and transpose

Transpose tutorials

Those should get you started. If you then post what you've tried, we'll be happy to help but for something that's clearly homework its a good idea to show what you've attempted first so we don't feel like we're doing it for you. Happy to help with any issues you encounter though.

@RRzio11 wrote:

I am using SAS University Edition on a Mac, I need help in how to to code per the questions after the dataset:

SAS Dataset

data psych;

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

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

;

runs;

In one data step --

• Create 10 new variables RQues1 ~ RQues10 by using ARRAY statement to reverse the values (e.g., 1 to 5, 2 to 4, etc.) of Ques1 to Ques10. Notice that original missing data are still missing in new variables.
• Create a new variable ToTQues by using SUM function to make a summation number from variables Ques1 to Ques10.
• Create a new variable Missing to count how many variables from Ques1 to Ques10 with missing data in each ID. (Hint: there is SAS function to count variables with missing data).
• 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. Notice that (65, 70, 60, 62, 68) should be defined in a temporary array.
• Rearrange the position of variables in the final data set by the following order:

ID Ques1 . . . Ques10 RQues1 . . . RQues10 ToTQues Missing Score1 . . . Score5

• Print all variables in the output window and copy to below.

Calcite | Level 5

## Re: SAS array and transpose

Thank you!

This is what I have so far:

Libname psych "/folders/myshortcuts/SASUniversity_Edition/MySASLib";
data psych;
input ID \$3. Ques1-Ques10 Score1-Score5;
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;

data psychRQ;
set psych;
RQues1= sum(Ques1);
RQues2= sum(Ques2);
RQues3= sum(Ques3);
RQues4= sum(Ques4);
RQues5= sum(Ques5);
RQues6= sum(Ques6);
RQues7= sum(Ques7);
RQues8= sum(Ques8);
RQues9= sum(Ques9);
RQues10= sum(Ques10);
run;
proc print data=psychRQ;
run;

data psych;
set psychRQ;
array areverse {10} Ques1-Ques10;
run;

data psychRQ;
set psych;
ToTQues= sum(Ques1-Ques10);
run;
proc print data=psychRQ;
run;

data psychRQ;
set psych;
ARRAY miss (10) _temporary_ (9 9 9 9 9);
ARRAY psych (10) Ques1-Ques10;
DO i = 1 TO 10;
IF psych(.) = miss(1) THEN psych(.) = 1;
END;
DROP i;
RUN;
Super User

## Re: SAS array and transpose

SUM() function doesn't do what you think it does. You need to use a PROC to summarize data for an entire data set. You can use SUM() when adding values across a single row. I would highly recommend commenting your code, I have no idea which step is supposed to answer which question for you. Start with just the very first portion only and solve that one.
Super User

## Re: SAS array and transpose

Why do you create the libref if you are not going to use it?

To reference a dataset using the libref you need to use a "two level" name. A name with a period in the middle.  If you use just a "one level" name then SAS will assume you want to use WORK as the libref. So this statement

``data psych;``

is the same as

``data work.psych;``

To store this original data into "permanent" dataset in the PSYCH libref (or "library") you would use something like:

``data psych.original;``

Discussion stats
• 4 replies
• 660 views
• 0 likes
• 3 in conversation