BookmarkSubscribeRSS Feed
katez01
Calcite | Level 5

Hi, 

I am trying to create a new variable based on a re-ordered variable that includes groups 1,2,3,4 only (not group 5). I have reordered the variable below, but cannot figure out how to create a new variable that includes groups 1,2,3,4. I want to then work with this new variable to run the rest of my coding...

 

data a; 

if class = 1 then class_new = 1;
if class = 2 then class_new = 2;
if class = 3 then class_new = 3;
if class = 4 then class_new = 5;
if class = 5 then class_new = 4;

run;

 

data a;

if class_new = 1,2,3,4 (is this correct?) then class_new_final= "final classes";

run;

 

2 REPLIES 2
PaigeMiller
Diamond | Level 26

@katez01 wrote:

 

data a;

if class_new = 1,2,3,4 (is this correct?) then class_new_final= "final classes";

run;

 


I think you want

 

if class_new in (1,2,3,4) then ...

but I really don't understand the part

 

then class_new_final= "final classes";

but maybe that doesn't really matter here.

 

Of course you could simplify the code even further by using working with the original variable named CLASS

 

if class in (1,2,3,5) then ...

and then you don't need all those IF statements.

 

Your data steps probably need a SET statement or an INPUT statement.

--
Paige Miller
Astounding
PROC Star

Presumably you already have a data set that contains CLASS.  To use that data set, you need to use a SET statement.  For example, if the name of the data set that contains CLASS is A, you need:

 

data new;

   set a;

   ... whatever changes you want to make, such as creating class_final ...;

run;

 

If you use this statement instead, you will destroy your existing data set:

 

data a;

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