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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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