Help using Base SAS procedures

New variable, reorder?

Reply
Occasional Contributor
Posts: 14

New variable, reorder?

Hi,

Anyone could help?


Data test;
set sample;
if Variable_1 >3.5 comments ="Old";
else Comments ="New";
run;

SAS would automatically put "Comments" at the end. If I already have 80 varialbes, "Comments" would be the 81th. How can I put "comments" in front of other varibles. So, when I export final results into excel, I don't have to scroll down to the end to see my comments.

I can use proc sql or retain statment to reorder my variables, but is there any better way? I have more than 80 varibles and I use proc import to import data, so every time the 80 varibles could have different names.
PROC Star
Posts: 7,492

Re: New variable, reorder?

Since you only want to move your Comments variable, that is the only variable that has to be on the retain or length statment. And, since you probably have to assign a length to the variable, I'd use the length statement to accomplish the task. Thus, the following should do what you want:

data test;
length Comments $ 250;
set sashelp.class (rename=(
age=Variable_1));
if Variable_1 >14 then comments ="Old";
else Comments ="New";
run;

HTH,
Art
Occasional Contributor
Posts: 14

Re: New variable, reorder?

Thanks art297.
Super User
Posts: 10,046

Re: New variable, reorder?

Hi.
art297's idea is great . I like it.
and I also have a method.You can change the order of variable as you want.

[pre]
data test;
set sashelp.class ;
if age >14 then comments ="Old";
else Comments ="New";
run;
proc report data=test nowd out=temp(drop=_break_);
column comments name -- weight; * name is your first variable ,weight is your last variable;
run;
proc print noobs;
run;
[/pre]



Ksharp
Regular Contributor
Posts: 241

Re: New variable, reorder?

@Ksharp: Re-ordering variables using PROC REPORT is new to me. It seems to variable-label as well, unless the var already has one. Interesting!
[pre]
data test;
set sashelp.class ;
if age >14 then comments ="Old";
else Comments ="New";
label age="age at last birthday";
run;

/* re-order vars using proc report -- it var labels as well */
proc report data=test nowd out=temp(drop=_break_);
column comments name -- weight;
run;

/* check */
proc compare data=test compare=temp;
run;
/* on lst -- in part
...
Number of Variables in Common: 6.
Number of Variables with Differing Attributes: 5.
...
NOTE: No unequal values were found. All values compared are exactly equal.
*/
[/pre]
Super User
Posts: 10,046

Re: New variable, reorder?

Posted in reply to chang_y_chung_hotmail_com
:-)
Yes. I am also curious that proc report would create variable label by using variable name automatically,when this variable has no label.And move the variable with it's label.

From your id name , I guess you are chinese boy ??
I am too.


Ksharp

Message was edited by: Ksharp
Ask a Question
Discussion stats
  • 5 replies
  • 294 views
  • 0 likes
  • 4 in conversation