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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 3 replies
  • 640 views
  • 0 likes
  • 2 in conversation