BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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?
3 REPLIES 3
deleted_user
Not applicable
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
LinusH
Tourmaline | Level 20
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
deleted_user
Not applicable
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

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

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1345 views
  • 0 likes
  • 2 in conversation