BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
martino1
Calcite | Level 5

Hello

 

Is there a way to create z1 with the value from x1 for a start, and then z2 with the value from z1, and z3 with the value from z2, but every time y changes z must again pick its value from x.

 

The problem is, that there are too many values in x, and I only need the first value from x every time y changes.

Data are sorted by column y.

 

In Excel it would be like this:

 

in cell z1:  =x1

in cell z2:  =if(y2=y1;z1;x2)

in cell z3:  =if(y3=y2;z2;x3)

in cell z4:  =if(y4=y3;z3;x4)

etc.

 

Data are big and I use SAS Enterprise Guide 7.11

 

For example:

 

Have:

   

 

x

y

z

1

788

7

 

2

415

7

 

3

964

8

 

4

375

8

 

5

105

8

 

6

105

8

 

7

105

8

 

8

105

9

 

9

105

9

 

    

Want:

   

 

x

y

z

1

788

7

788

2

415

7

788

3

964

8

964

4

375

8

964

5

105

8

964

6

105

8

964

7

105

8

964

8

105

9

105

9

105

9

105

 

Thank you 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Yes, you can easily use BY group processing here. If you can't sort your data for some reason, you can use the NOTSORTED option. 

 

 

data want;
set have;

by Y NOTSORTED;

retain z;

if first.y then z = x;

run;

@martino1 wrote:

Hello

 

Is there a way to create z1 with the value from x1 for a start, and then z2 with the value from z1, and z3 with the value from z2, but every time y changes z must again pick its value from x.

 

The problem is, that there are too many values in x, and I only need the first value from x every time y changes.

Data are sorted by column y.

 

In Excel it would be like this:

 

in cell z1:  =x1

in cell z2:  =if(y2=y1;z1;x2)

in cell z3:  =if(y3=y2;z2;x3)

in cell z4:  =if(y4=y3;z3;x4)

etc.

 

Data are big and I use SAS Enterprise Guide 7.11

 

For example:

 

Have:

     

 

x

y

z

1

788

7

 

2

415

7

 

3

964

8

 

4

375

8

 

5

105

8

 

6

105

8

 

7

105

8

 

8

105

9

 

9

105

9

 

       

Want:

     

 

x

y

z

1

788

7

788

2

415

7

788

3

964

8

964

4

375

8

964

5

105

8

964

6

105

8

964

7

105

8

964

8

105

9

105

9

105

9

105

 

Thank you 🙂


 

View solution in original post

3 REPLIES 3
Reeza
Super User

Yes, you can easily use BY group processing here. If you can't sort your data for some reason, you can use the NOTSORTED option. 

 

 

data want;
set have;

by Y NOTSORTED;

retain z;

if first.y then z = x;

run;

@martino1 wrote:

Hello

 

Is there a way to create z1 with the value from x1 for a start, and then z2 with the value from z1, and z3 with the value from z2, but every time y changes z must again pick its value from x.

 

The problem is, that there are too many values in x, and I only need the first value from x every time y changes.

Data are sorted by column y.

 

In Excel it would be like this:

 

in cell z1:  =x1

in cell z2:  =if(y2=y1;z1;x2)

in cell z3:  =if(y3=y2;z2;x3)

in cell z4:  =if(y4=y3;z3;x4)

etc.

 

Data are big and I use SAS Enterprise Guide 7.11

 

For example:

 

Have:

     

 

x

y

z

1

788

7

 

2

415

7

 

3

964

8

 

4

375

8

 

5

105

8

 

6

105

8

 

7

105

8

 

8

105

9

 

9

105

9

 

       

Want:

     

 

x

y

z

1

788

7

788

2

415

7

788

3

964

8

964

4

375

8

964

5

105

8

964

6

105

8

964

7

105

8

964

8

105

9

105

9

105

9

105

 

Thank you 🙂


 

Astounding
PROC Star
An easy way...

Data want;
set have;
by y notsorted;
if first.y then z=x;
retain z;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1061 views
  • 4 likes
  • 3 in conversation