BookmarkSubscribeRSS Feed
Sidhu
Calcite | Level 5

How to change order of observations in dataset without using proc procedures like proc sort, proc sql etc.

What I want to ask ... is there any way to shuffle the dataset. As this is question I faced by interview.

For example :

In one dataset :

Subid Invid Sex Age

0001 101   Male 30

0002 202   Male 40

0003 303   Male 50

0004 404  Female 60

The output dataset should be like.

Subjid Invid Sex Age

0004 404 Female 60

0001 101 Male    30

0003 303 Male    50

0002 202 Male    40

2 REPLIES 2
Tom
Super User Tom
Super User

Stupid question.

To get that exact result you could use WHERE clauses on SET statements.

data want ;

  set have (where=(sex='Female'))

      have (where=(sex='Male'))

  ;

run;


If they wanted to know if you knew how to use dataset options they should have just asked a question about that.

PGStats
Opal | Level 21

Agreed, but you get order subid=4,1,2,3 and 4,1,3,2 was requested. Maybe they wanted

data want;

do i = 1 to ceil(last/2);

     p = last - i + 1;

     set have point=p nobs=last;

     output;

     if i < p then do;

          set have point=i;

          output;

          end;

     end;

stop;

drop i;

run;

Smiley Happy PG

PG

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 2 replies
  • 2371 views
  • 0 likes
  • 3 in conversation