DATA Step, Macro, Functions and more

Retain observaions with values

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 114
Accepted Solution

Retain observaions with values

Hi SAS Experts,

 

I want to retain the observations of a data set with the corresponding values. Below is the dataset.

 


data a;
input div$ marks;
datalines;
middle 88
low 55
end 35
start 99
;
run;

 

I want the output like this.

 

div marks
start 99
middle 88
low 55
end 35

 

Please help

 

Thanks & Regards,

Sanjay


Accepted Solutions
Solution
‎06-21-2017 05:30 AM
Trusted Advisor
Posts: 1,128

Re: Retain observaions with values

it would be good to create an ord variable using informat and sort data like below

 

data a;
input element$ score;
datalines;
end 800
middle 1000
start 700
Low 600
;
run;

proc format;
invalue ord
'start'=1
'middle'=2
'Low'=3
'end'=4;
run;

data b;
set a;
ord=input(element,ord.);
run;

proc sort data=b;
by ord;
run;
Thanks,
Jag

View solution in original post


All Replies
Trusted Advisor
Posts: 1,128

Re: Retain observaions with values

[ Edited ]

I don't see any difference between the a (data with input statement) and want dataset. Could you please elaborate your question

Thanks,
Jag
PROC Star
Posts: 252

Re: Retain observaions with values

[ Edited ]

this needs an order by statement. I am not sure what you are looking for. It is missing peoper order which is there in @art297 code

 

proc sql;

select * from a

order by marks;

quit;

PROC Star
Posts: 7,363

Re: Retain observaions with values

Are you only trying to reorder the data in descending order according to the values of marks?

 

data a;
input div$ marks;
datalines;
middle 88
low 55
end 35
start 99
;
run;
proc sql; create table want as select * from a order by marks descending ; quit;

Art, CEO, AnalystFinder.com

Trusted Advisor
Posts: 1,128

Re: Retain observaions with values

[ Edited ]

If you want to order the data like what @kiranv_ mentioned then you could try proc sort like below 

 

proc sort data=a out=want;

by descending marks;

run;

Thanks,
Jag
Super User
Posts: 5,082

Re: Retain observaions with values

Maybe.  Or maybe it should be sorted like this:

 

proc sort data=a out=want;

by descending div;

run;

 

It's really up to the poster to tell us what "like this" means instead of relying on us to guess.

Frequent Contributor
Posts: 114

Re: Retain observaions with values

Hi Astounding,

 

Sorry for the confusion,  below is the example data set.

 

data a;
input element$ score;
datalines;
end 800
middle 1000
start 700
Low 600
;
run;

 

 

desired output:

Start 700

middle 1000

low 600

end 800

 

Actually am trying to draw a waterfall chart which starts from Start,middle,low and end.

Solution
‎06-21-2017 05:30 AM
Trusted Advisor
Posts: 1,128

Re: Retain observaions with values

it would be good to create an ord variable using informat and sort data like below

 

data a;
input element$ score;
datalines;
end 800
middle 1000
start 700
Low 600
;
run;

proc format;
invalue ord
'start'=1
'middle'=2
'Low'=3
'end'=4;
run;

data b;
set a;
ord=input(element,ord.);
run;

proc sort data=b;
by ord;
run;
Thanks,
Jag
Frequent Contributor
Posts: 114

Re: Retain observaions with values

Thank you jagadish, it works well for me.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 151 views
  • 1 like
  • 5 in conversation