DATA Step, Macro, Functions and more

loop

Reply
N/A
Posts: 0

loop

I want to convert following sorted data set to:

VAR1 VAR1 VAR2
laden laden 1
tussen tussen 1
lossen lossen 1
laden ===>> laden 2
tussen tussen 2
lossen lossen 2
lossen lossen 2
laden laden 3
lossen lossen 3

where a VAR2 is a variable that is a new cycle each time VAR1 is 'laden'.

which code do I need for this conversion?
N/A
Posts: 0

Re: loop

Is this what you are after?

data after;
set before;
*if var1 is laden add one to the new variable;
*this syntax will automatically retain the value of var2;
*and will not give you a missing value if the value of var2 is missing;
if var1='laden' then var2+1;
run;

I haven't run this but it should work. forgot to put quotes round the literal. Done now.


Message was edited by: pznew
Super User
Posts: 5,257

Re: loop

For this construct you need to have VAR2 retained between observations:


data after;
set before;
retain var2 0;
if var1 eq 'laden' then var2+1;
run;

/Linus
Data never sleeps
N/A
Posts: 0

Re: loop

As I mention in the code comment. The sum statement will automatically retain the variable. You can put the retain in for completeness but it is not required.

P
Ask a Question
Discussion stats
  • 3 replies
  • 126 views
  • 0 likes
  • 2 in conversation