BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Linlin
Lapis Lazuli | Level 10

Hi All

How could I get dataset WANT from HAVE?  Thank you!

 

data have;
input id$ count var1;
cards;
aa 1 20
aa 2 .
aa 3 30
bb 1 10
bb 2 .
bb 3 .
bb 4 20
cc 1 10
cc 2 .
cc 3 30
cc 4 .
cc 5 50
;

 

data want;

id count var1
aa 1 20
aa 2 25
aa 3 30
bb 1 10
bb 2 15
bb 3 15
bb 4 20
cc 1 10
cc 2 20
cc 3 30
cc 4 40
cc 5 50

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

A data step solution:

 

data have;
input id$ count var1;
cards;
aa 1 20
aa 2 .
aa 3 30
bb 1 10
bb 2 .
bb 3 .
bb 4 20
cc 1 10
cc 2 .
cc 3 30
cc 4 .
cc 5 50
;

data want0;
retain lastVar1;
set have; by id;
if first.id then call missing(lastVar1);
if missing(var1) then prevVar1 = lastVar1;
else lastVar1 = var1;
drop lastVar1;
run;

proc sort data=want0; by id descending count; run;

data want;
retain lastVar1;
set want0; by id;
if first.id then call missing(lastVar1);
if missing(var1) then var1 = mean(prevVar1, lastVar1);
else lastVar1 = var1;
drop lastVar1 prevVar1;
run;

proc sort data=want; by id count; run;

proc print data=want noobs; run;
PG

View solution in original post

4 REPLIES 4
Haikuo
Onyx | Level 15

Long time no see! I suppose Data step solution would be a lot more efficient/elegant, but I can't figure out one right now, so here is the Ugly.

 

data have;
input id$ count var1;
cards;
aa 1 20
aa 2 .
aa 3 30
bb 1 10
bb 2 .
bb 3 .
bb 4 20
cc 1 10
cc 2 .
cc 3 30
cc 4 .
cc 5 50
;
 
proc sql;
create table want as
select id, count, case when not missing(var1) then var1 else mean((select var1 from have where id=a.id and count < a.count and not missing(var1) having count=max(count)),
(select var1 from have where id=a.id and count > a.count and not missing (var1) having count=min(count))) end as var1
from have a
;
quit;
art297
Opal | Level 21

Also, long time, no see. Missed you!

 

You appear to want to use two different methods for the 2nd and 3rd IDs. Is that really what you want, or something else?

If you want to use the same method, the following seems to come close:

 

PROC STDIZE data=have out=want method=mean missing=midrange reponly;
  var var1;
  by id;
run;

 

Art, CEO, AnalystFinder.com

 

PGStats
Opal | Level 21

A data step solution:

 

data have;
input id$ count var1;
cards;
aa 1 20
aa 2 .
aa 3 30
bb 1 10
bb 2 .
bb 3 .
bb 4 20
cc 1 10
cc 2 .
cc 3 30
cc 4 .
cc 5 50
;

data want0;
retain lastVar1;
set have; by id;
if first.id then call missing(lastVar1);
if missing(var1) then prevVar1 = lastVar1;
else lastVar1 = var1;
drop lastVar1;
run;

proc sort data=want0; by id descending count; run;

data want;
retain lastVar1;
set want0; by id;
if first.id then call missing(lastVar1);
if missing(var1) then var1 = mean(prevVar1, lastVar1);
else lastVar1 = var1;
drop lastVar1 prevVar1;
run;

proc sort data=want; by id count; run;

proc print data=want noobs; run;
PG
Linlin
Lapis Lazuli | Level 10

Hi Haikuo, Art, and PG,

Thank you very much for your great help!! I miss you guys too.

Best wishes!

Linlin

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 1200 views
  • 3 likes
  • 4 in conversation