06-09-2016 02:35 PM - edited 06-09-2016 02:37 PM
I have two numeric variables, AR and SR that I want to assign a "type" to. When I write the following statement:
if AR > 0 then type='2'; if SR > 0 then type= '1'; Note: all AR and SR values are greater than 0.
I receive an error regarding the "then" statement; if I remove it, I receive another error. I am trying to create a categorical value that I can use in a class statement for analysis.
Can anyone help?
06-09-2016 02:58 PM
Your IF/THEN statements appear to be OK as is. You will need to show us the context in which you are using them ... perhaps the messages that you received as well.
06-09-2016 03:02 PM
SAS Variables do not change type. However there is a very easy way to create categories for ranges of numerical variables using SAS Formats. Suppose you have a numeric variable with a range from 1 to 10 (integers) and want to create 3 groups for analysis.
/*mygroup is the name of the format, can't end in a number*/
1 - 3 = 'Low'
4 - 7 = 'Mid'
8 -10= 'High'
proc freq data=have;
format myvariable mygroup.;
and the tables will show Low, Mid and High.
Most of the analysis procedures will honor the groups designated by the format. An advantage to this approach is you can create multiple formats and don't have to change the values of anything in the dataset just assign the variable at analysis times. I do that with peoples age and have groups that are 5 year groups, 10 year groups, groups at specific age points based reporting recquirements.
06-09-2016 06:05 PM
I tried that, but I received error messages as well. Here is my code:
I then ram an analysis for the type variable, but SAS did not recognize the variable type.
06-09-2016 10:14 PM
Look at the log for the proc format code. The value statement includes the name, options, values and assigned values. You should have gotten an error about the lines
1) SR and AR are your variable names, the procedure explects VALUES. This is so the same mapping can be applied to many variables with the same behavior.
2) Your ; was in the wrong place.
I misunderstood a little of what you are doing. Does your variable TYPE already exsit at the point you are recoding based on the values of AR and SR? If so the issue could well be that TYPE is NUMERIC and you are attempting to assing a TEXT value '2' or '1'.
You may need to think a bit more about what you are attempting. You state
"Note: all AR and SR values are greater than 0."
but you are attempting to set a single variable Type to two different values that conflict with the above statement.
Please provide a few rows of example input data that demonstrated all of the cases of the logic involved and what the results should look like for that example data.
It may not hurt to post the Log results of the first code you ran along with the error messages.