Help using Base SAS procedures

Re ordering variables in sas

Reply
Occasional Contributor
Posts: 13

Re ordering variables in sas

Can anyone tell me how to reorder some variables if I am having large number of variables in my data set.

Super User
Posts: 5,429

Re: Re ordering variables in sas

Posted in reply to kumarnaidu

There are lot of threads discussing this issue. Use the search facility to fins them.

Data never sleeps
Super User
Posts: 10,028

Re: Re ordering variables in sas

Posted in reply to kumarnaidu

Give us an example, then someone will help you .

Occasional Contributor
Posts: 13

Re: Re ordering variables in sas

suppose I am having variables in the order- visit patno pat_initials body_temp1 sbp1 dbp1 pulserate1 body_temp2 sbp2 dbp2 pulserate2

and I want to arrange these variables in the order-patno visit pat_initials body_temp1 body_temp2 sbp1 sbp2 dbp1 dbp2 pulserate1 pulserate2.

It is just an example.

Super User
Posts: 10,028

Re: Re ordering variables in sas

Posted in reply to kumarnaidu

OK. You can create a format to customize the order of variables.

data have;
input (visit patno pat_initials body_temp1 sbp1 dbp1 pulserate1 body_temp2 sbp2 dbp2 pulserate2) ($);
cards;
1 2 3 4 5 3 4 5 4 3 2 2 2 2 2 2 2 2 2 2 2 2 2 
;
run;
proc sql;
 select name into : list separated by ' '
  from dictionary.columns
   where libname='WORK' and memname='HAVE' and name not in ('visit' 'patno' 'pat_initials')
    order by scan(name,1,' ','d'),input(scan(name,1,' ' ,'kd'),best32.);
quit;
%put &list;
data want;
 retain patno visit pat_initials &list;
 set have;
run;

Ksharp

Occasional Contributor
Posts: 13

Re: Re ordering variables in sas

thanks a lot

Super Contributor
Posts: 349

Re: Re ordering variables in sas

Posted in reply to kumarnaidu

Hi Kumar,

You can use length statement(if you define length for particular column it will come first ,if you don't want to change the order then don't define that variable in length statement) or you can use Column  in proc report.

Thanks,

Shiva

Valued Guide
Posts: 765

Re: Re ordering variables in sas

Hi ... one issue with using LENGTH is making sure that the lengths provided for the variables match those already in the data set that is being reordered.  I think that one is safer with the more standard solution that Ksharp proposed, using a RETAIN statement where no attributes of the variables are needed,

Ask a Question
Discussion stats
  • 7 replies
  • 301 views
  • 0 likes
  • 5 in conversation