@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".
... View more