## REORDERING VARIABLES

Solved
Occasional Contributor
Posts: 12

# REORDERING VARIABLES

Hello:

How can I change the order of my variables below to:

ID Ques1 - Ques10  RQues1 - RQues10  ToTQues  Missing  Score1 - Score5   ToTPass1 -ToTPass5 .

Thank you

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);

array S

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

array P

• Pass1 - Pass5;
•   array T

• TotPass1 - TotPass5;
•   retain TotP:;

do i = 1 to dim(S);

P = (S >= F);

T + P;

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;

Accepted Solutions
Solution
‎06-28-2015 10:06 PM
Super User
Posts: 8,093

## Re: REORDERING VARIABLES

Since you are creating the variables in that step just define them in the order that you want.  Note that having set the length (and type) will also make your INPUT statement simpler to write. You probably do not want to use a RETAIN statement to set the order in this step since you have many variables that are not part of the input and so if you mark them as retained the values will not get reset to missing when you being the step again to read the next row of the input data.

data psych;

length ID \$3 Ques1 - Ques10  RQues1 - RQues10  ToTQues  Missing  Score1 - Score5   ToTPass1 -ToTPass5 8 ;

input ID Ques1-Ques10 Score1-Score5;

...

All Replies
Posts: 4,736

## Re: REORDERING VARIABLES

The order of the variables in a data set is not really important. I assume this is mainly about convenience for you so that a simple "Proc Print" lists the variable in your desired order without you having to specify them in a Var statement.

The easiest way is to define the variables in an ATTRIB statement before you use them.

data psych;

attrib

ID length=\$3 informat=\$3.

ques1-ques10 length=8 informat=best32.

TotQues .....

;

input ID Ques1-Ques10 Score1-Score5;

ARRAY RQues (10)RQues1-RQues10;

......

If the data set already exists and you just want to re-order the variables in it then create a "mapping data set" with zero rows.

data test;

attrib id length=\$3

ques1-ques10 length=8

ToTQues ....

;

stop;

run;

data psych;

set mapping psych;

.....

run;

Occasional Contributor
Posts: 12

## Re: REORDERING VARIABLES

Patrick;  could you do it using the RETAIN statement?

Thank you

New Contributor
Posts: 4

## Re: REORDERING VARIABLES

Yes, you can do it with a retain statement, like in the following:

data psych;

retain ID Ques1-Ques10 RQues1-RQues10 ToTQues Missing

Score1-Score5 ToTPass1-ToTPass5 pass1-Pass5;

drop i;

...

But the most correct way to write SAS code is putting an attrib statement at the beginning of the data step with all the variables, their types and labels. And then a keep statement at the end of the step. It is the best way to tell clearly to anyone who might read your code what are the most important variables, their order, attributes, and if they will be stored in the final data set.

Posts: 4,736

## Re: REORDERING VARIABLES

Using an ATTRIB statement is imho cleaner as it allows you to fully specify how the variables get constructed (type, length, format, etc.). Using a retain also bears the risk that values get retained where you don't want them retained.

Solution
‎06-28-2015 10:06 PM
Super User
Posts: 8,093

## Re: REORDERING VARIABLES

Since you are creating the variables in that step just define them in the order that you want.  Note that having set the length (and type) will also make your INPUT statement simpler to write. You probably do not want to use a RETAIN statement to set the order in this step since you have many variables that are not part of the input and so if you mark them as retained the values will not get reset to missing when you being the step again to read the next row of the input data.

data psych;

length ID \$3 Ques1 - Ques10  RQues1 - RQues10  ToTQues  Missing  Score1 - Score5   ToTPass1 -ToTPass5 8 ;

input ID Ques1-Ques10 Score1-Score5;

...

Occasional Contributor
Posts: 12