## how to sort data without using proc step and first.var and last.var?

Solved
Occasional Contributor
Posts: 13

# how to sort data without using proc step and first.var and last.var?

I've dataset name called sales

I want sort data without using proc and first.var and last.var;

data tab;

input country;

cards;

1

3

2

5

6

1

5

4

;

run;

Accepted Solutions
Solution
‎02-27-2017 05:30 AM
Super User
Posts: 10,778

## Re: how to sort data without using proc step and first.var and last.var?

Maybe they just want a computer science guy who know data structure. not a sas or statististic guy.

This kind of question should be given to software developer . not data analysis.

``````data tab;
input country;
cards;
1
3
2
5
6
1
5
4
;
run;

data want;
set tab end=last;
array x{99999} _temporary_;
x{_n_}=country;
if last then do;
do i=1 to _n_;
do j=i+1 to _n_;
if x{i} gt x{j} then do;
temp=x{i};x{i}=x{j};x{j}=temp;
end;
end;
end;

do k=1 to _n_;
country=x{k};output;
end;
end;
keep country;
run;``````

All Replies
Super User
Posts: 10,238

## Re: how to sort data without using proc step and first.var and last.var?

What is the reason for not using proc sort?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 13

## Re: how to sort data without using proc step and first.var and last.var?

it was one of the interview question that i was asked for..

Super User
Posts: 10,238

## Re: how to sort data without using proc step and first.var and last.var?

You can produce sorted data with a hash object (probably the answer the interviewer wanted).

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 9,599

## Re: how to sort data without using proc step and first.var and last.var?

"

I want sort data without using proc and first.var and last.var;" - How will first/last help you sort anything?

Use one of the methods developed by experts over years = proc sort, order by.  These are both efficient and simple, and all SAS programmers will be abel to understand your code easily.

Alternatively, get yourself and x86 assembler and write all your code using machine code.

Solution
‎02-27-2017 05:30 AM
Super User
Posts: 10,778

## Re: how to sort data without using proc step and first.var and last.var?

Maybe they just want a computer science guy who know data structure. not a sas or statististic guy.

This kind of question should be given to software developer . not data analysis.

``````data tab;
input country;
cards;
1
3
2
5
6
1
5
4
;
run;

data want;
set tab end=last;
array x{99999} _temporary_;
x{_n_}=country;
if last then do;
do i=1 to _n_;
do j=i+1 to _n_;
if x{i} gt x{j} then do;
temp=x{i};x{i}=x{j};x{j}=temp;
end;
end;
end;

do k=1 to _n_;
country=x{k};output;
end;
end;
keep country;
run;``````
☑ This topic is solved.