BookmarkSubscribeRSS Feed
MirLuie
Calcite | Level 5
hey there!

after 5hours of trying i hope u can help me 😕

In data step i like to keep a value x of a column X that has a y value until i reach the "last" value with same y value of column Y. I tried with lag() and retain() but i did not succeed.

in a logic way it should be like: keep the x1 where Y is y1 and x1 is before y2 until the y1 that is before y2 and write x1 for x5 in X

data set is like this:

Y X
y1 x1
y2 x2
y1 x3
y1 x4
y1 x5
y2 x6


i hope soneone please coukd help me?

thank you!
6 REPLIES 6
Reeza
Super User
Show what you want as output please, it's not clear from your post.
MirLuie
Calcite | Level 5

i dont know what u mean by show it in output?

 

let's say it very simple: i want to use a value later in a data set.. 

Reeza
Super User

What do you want the output data set to look like? 


@MirLuie wrote:

i dont know what u mean by show it in output?

 

let's say it very simple: i want to use a value later in a data set.. 


 

ErikLund_Jensen
Rhodochrosite | Level 12

Hi

As I understand your problem, it is because the input data is not sorted on y and x.

 

The supplied code gives 2 output records: Y1 X1 and Y2 X2

 

data have;
	input Y$ X$;
datalines;
y1 x1
y2 x2
y1 x3
y1 x4
y1 x5
y2 x6
;
run;

proc sort data=have; by Y X;
run;

data want; set have; by Y;
	retain wantx;
	if first.y then wantx = x;
	if last.y then output;
run;

 

MirLuie
Calcite | Level 5
hey erik!

thanks for your suggestion.. (un)fortunately i solved my problem with lag() and delete () but I will try ur solution later... thank you very much!
mkeintz
PROC Star

I would like to see your lag/delete solution to this problem.  Please post it and mark your topic solved.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 1890 views
  • 0 likes
  • 4 in conversation