DATA Step, Macro, Functions and more

Retain Statement Help

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Retain Statement Help

I have a dataset with values

as A,A,B,B,Z,Z,ZC,C,Z,Z

 

The output should be like this A,A,B,B,B,B,B,C,C,C,C

 


Accepted Solutions
Solution
‎09-23-2016 08:46 AM
Super User
Posts: 17,724

Re: Retain Statement Help

Data want;

set have;

 

retain new_var;

 

if var ne 'Z' then new_var=var;

 

run;

 

 

View solution in original post


All Replies
Super User
Posts: 17,724

Re: Retain Statement Help

Explain your logic and problem in significantly more detail. 

Occasional Contributor
Posts: 18

Re: Retain Statement Help

Hi I have  a dataset "Products" which has a variable "Prod". This variable has got values 

A

A
B

Z

Z

C

C

Z

Z

Whenever Z value comes up, like in this case Z has come up 4th observation, so it has to retain preceding value (B) and then again 5th observation is Z, since 4th obs has been replaced the 5th obs has also to be replaced with prior observation. When ever Z pops up it has to take preceding value

so the output is

A

A

B

B

C

C

C

C

Solution
‎09-23-2016 08:46 AM
Super User
Posts: 17,724

Re: Retain Statement Help

Data want;

set have;

 

retain new_var;

 

if var ne 'Z' then new_var=var;

 

run;

 

 

Occasional Contributor
Posts: 18

Re: Retain Statement Help

thanks 

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 226 views
  • 1 like
  • 2 in conversation