I have a input dataset like below
input dataset
Name
A
B
E
F
D
C
Need a datase like below
output dataset
Name
C
D
F
E
B
A
Please help me write this query.
Thanks in advance priya
Hi,
I am afraid you have no option other than to put the values in yourself. There appears to be no logical reason why A is last and C is first for instance, so you cannot write a logical step to do this.
So, putting the information in yourself, you have several options. Datastep, if statements, user formats. Me, I would go with an array:
data want;
set have;
array letters{26} ("C","D","F",...); /* In the order you want */
do I=1 to 26;
if letters{I}=name then var_id=I;
end;
run;
Then sort by var_id.
step1:craete a new variable , a=_N_
step2: sort the above by variable a DESC;
Unfortunately its not descending though, F - E - B? It just looks random. Unless there is further information - such as the data was sorted that way prior, then you could use _n_ and then re-sort back. However that's not stated in the post. And the question would be, why? What value is there having some arbitrary sort order without logic. If the data changes the order might change and then you would not be able to re-create it.
If this is just about getting your source data set in reversed order without having a variable for sorting then below code could do.
data have;
input Name $;
datalines;
A
B
E
F
D
C
;
run;
data want;
do i=nobs to 1 by -1;
set have nobs=nobs point=i;
output;
end;
stop;
run;
I agree with Patrick. Gsreddy solution would be something like this:
data temp / view=temp;
set sashelp.class;
a=_N_;
run;
proc sort data=temp out=class(drop=a);
by descending a;
run;
Ctorres
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.