Help using Base SAS procedures

How do i asending the oder of n number of visit coloumn ?

Reply
Occasional Contributor
Posts: 17

How do i asending the oder of n number of visit coloumn ?

Hi,

How do i asending the oder of n number of visit coloumn ?

Example:-

DATA VAR;

INPUT Subject $ VISIT1 VISIT3 VISIT2_01 VISIT2 VISIT3_02 VISIT3_01

DATALINES;

A0001 5 6 2 3 54 2

B0001 2 4 6 5 4 5

C0001 2 4 36 5 4 2;

run;

WANT -  

oderwise variables-

Subject VISIT1 VISIT2 VISIT2_01 VISIT3 VISIT3_01 VISIT3_01

                                            
Regards,

Rohit

PROC Star
Posts: 7,356

Re: How do i asending the oder of n number of visit coloumn ?

Since you appear to only want them sorted alphabetically, your easiest approach (I think) would be to use proc sql to generate the desired ordering, and then use that info in a retain statement.  e.g.:

DATA VAR;

   INPUT Subject $ VISIT1 VISIT3 VISIT2_01 VISIT2 VISIT3_02 VISIT3_01;

   DATALINES;

A0001 5 6 2 3 54 2

B0001 2 4 6 5 4 5

C0001 2 4 36 5 4 2

;

run;

proc sql noprint;

  select name

    into :names separated by " "

      from dictionary.columns

        where libname eq "WORK" and

              memname eq "VAR"

          order by name

  ;

quit;

data want;

  retain &names.;

  set var;

run;

Super Contributor
Posts: 282

Re: How do i asending the oder of n number of visit coloumn ?

Hi Rohit,

Were you looking for something like the following:

DATA VAR;

length Subject   $8

       VISIT1    $8

       VISIT2    $8

       VISIT2_01 $8

       VISIT3    $8

       VISIT3_01 $8

       VISIT3_02 $8

;

INPUT Subject $ VISIT1 VISIT3 VISIT2_01 VISIT2 VISIT3_02 VISIT3_01;

DATALINES;

A0001 5 6 2 3 54 2

B0001 2 4 6 5 4 5

C0001 2 4 36 5 4 2

;

run;

Regards,

Amir.

EDIT: I just realised you've probably been given the SAS data set in that order, so you can ignore my response if that's the case.

Message was edited by: Amir Malik

Respected Advisor
Posts: 3,886

Re: How do i asending the oder of n number of visit coloumn ?

The order of variables in a dataset should be irelevant - but here you go:

DATA VAR;
   INPUT Subject $ VISIT1 VISIT3 VISIT2_01 VISIT2 VISIT3_02 VISIT3_01;
   DATALINES;
A0001 5 6 2 3 54 2
B0001 2 4 6 5 4 5
C0001 2 4 36 5 4 2
;
run;

proc sql noprint;
   select name into Smiley FrustratedortedVarList separated by ' '
   from dictionary.columns
   where libname='WORK' and memname='VAR' and upcase(name) like 'VISIT%'
   order by name
   ;
quit;

data VAR;
   retain Subject &SortedVarList;
   set var;
run;

Super Contributor
Posts: 276

Re: How do i asending the oder of n number of visit coloumn ?

Hi Rohit.

similar kind question i posted long back...

Go through bellow link,you can find some other ways...

https://communities.sas.com/thread/32930

Regards...

Sanjeev.K

Super User
Posts: 9,657

Re: How do i asending the oder of n number of visit coloumn ?

As Arthur and Patrick did, You can get whatever order you want by using SQL.

DATA VAR;
   INPUT Subject $ VISIT1 VISIT3 VISIT2_01 VISIT2 VISIT3_02 VISIT3_01;
   DATALINES;
A0001 5 6 2 3 54 2
B0001 2 4 6 5 4 5
C0001 2 4 36 5 4 2
;
run;

 

proc sql noprint;
   select name into :SortedVarList separated by ' '
   from dictionary.columns
   where libname='WORK' and memname='VAR' and upcase(name) like 'VISIT%'
   order by input(scan(name,1,'VISIT_'),best8.),input(scan(name,2,'VISIT_'),best8.)
   ;
quit;

%put &SortedVarList ;
 

data VAR;
   retain Subject &SortedVarList;
   set var;
run;

Ksharp

Ask a Question
Discussion stats
  • 5 replies
  • 182 views
  • 0 likes
  • 6 in conversation