BookmarkSubscribeRSS Feed
rohit_prajapati
Calcite | Level 5

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

5 REPLIES 5
art297
Opal | Level 21

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;

Amir
PROC Star

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

Patrick
Opal | Level 21

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 :SortedVarList 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;

kuridisanjeev
Quartz | Level 8

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

Ksharp
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 704 views
  • 0 likes
  • 6 in conversation