BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
raselle
Fluorite | Level 6

Hi all,

 

I am trying to use an if statement to create a new column variable in my datasets:

 

*I imported the csv file so the input here is just for demonstration;

input RoundNo;

datalines;

1

10

11

3

4

;

 

data want;
set have;
       if RoundNo <= 9 then round_cat = "early";
else
       round_cat = "late_stage";
run;

 

When I check my output, instead of "late_stage", I only got "late_" so I am confused about what happened here. Can someone help me with this ? Thank you.

 

1 ACCEPTED SOLUTION

Accepted Solutions
KachiM
Rhodochrosite | Level 12

Welcome to SAS Community.

 

data want;
set have;
       if RoundNo <= 9 then round_cat = "early";
else
       round_cat = "late_stage";
run;

The length of ROUND_CAT is assumed to be the length of EARLY as it occurs first. Since LATE_STAGE is longer than EARLY, ROUND_CAT holds the length of EARLY- that is 5 characters from LATE_STAGE.

 

You can use:

Length ROUND_CAT $ 10

to hold both the labels

 

View solution in original post

7 REPLIES 7
KachiM
Rhodochrosite | Level 12

Welcome to SAS Community.

 

data want;
set have;
       if RoundNo <= 9 then round_cat = "early";
else
       round_cat = "late_stage";
run;

The length of ROUND_CAT is assumed to be the length of EARLY as it occurs first. Since LATE_STAGE is longer than EARLY, ROUND_CAT holds the length of EARLY- that is 5 characters from LATE_STAGE.

 

You can use:

Length ROUND_CAT $ 10

to hold both the labels

 

raselle
Fluorite | Level 6

That worked perfectly! Thank you

Kurt_Bremser
Super User

@raselle wrote:

Hi guys,

 


Note that we're mixed gender here, and that the most prolific solution provider is not a "guy", so she has made it a point to never answer questions that begin like that.

raselle
Fluorite | Level 6

I'm a non-native speaker and thought that "guys" is a gender-neutral words. I will keep that in mind next time. Thank you

raselle
Fluorite | Level 6

I will use that from now then. Smiley Very Happy

ballardw
Super User

@raselle wrote:

Hi guys,

 

I am trying to use an if statement to create a new column variable in my datasets:

 

*I imported the csv file so the input here is just for demonstration;

input RoundNo;

datalines;

1

10

11

3

4

;

 

data want;
set have;
       if RoundNo <= 9 then round_cat = "early";
else
       round_cat = "late_stage";
run;

 

When I check my output, instead of "late_stage", I only got "late_" so I am confused about what happened here. Can someone help me with this ? Thank you.

 


Many times you can avoid creating new variables if the value you need is based on single variable by creating a custom format and using that as needed. The following code creates an example data set of values and two custom formats. Then uses proc print to display the same variable with the two formats applied.

data work.example;
   do x= 1 to 15;
   output;
   end;
run;

proc format library=work;
value early
low - 9 = "Early"
other = "Late stage"
;
value olevels
0 - 3 = '[0,3]'
3<- 6 = '(3,6]'
6<- high='>6'
;
run;

Title "X with format Early";
proc print data=work.example;
   var x;
   format x early.;
run;
Title "X with format Olevels";
proc print data=work.example;
   var x;
   format x olevels.;
run;title;

Formats like this are honored for most analysis, graphing or reporting procedures to not only display values but create groups for analysis or display.

 

Also for some sorts of code with lots of ranges the code in Proc Format can be much easier to write/maintain then a bunch of "if then else" statements. Once you have a format created you can apply it to many variables that might want similar behavior. Imagine if you have many RoundNo type variables. You want have to create a separate text variable for each group, keep the names and possibly labels straight and other fiddly bits. With the format it is just change the format statement and go.

Formats also work with character variables though attempting to use ranges of values is usually very problematic. Imagine that  you have a list of Office names that you want to treat as a group for some purposes such as a management region. Instead of coding a bunch of if/then/else statements a format works just fine. And when a new office is opened you just add it to the format. Not other code needs to change. Or if an office changes management region change the format to show that change. Of if the office closes you can have a value of the format that points to "closed branches".

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 501 views
  • 3 likes
  • 4 in conversation