DATA Step, Macro, Functions and more

error message when I use the "then" statement

Reply
New Contributor
Posts: 2

error message when I use the "then" statement

[ Edited ]

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?

thanks,

Maureen

 

Super User
Posts: 5,085

Re: error message when I use the "then" statement

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.

Super User
Posts: 10,516

Re: error message when I use the "then" statement

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.

 

Proc format;

/*mygroup is the name of the format, can't end in a number*/

Value Mygroup

1 - 3 = 'Low'

4 - 7 = 'Mid'

8 -10= 'High'

;

run;

 

proc freq data=have;

    tables myvariable;

    format myvariable mygroup.;

run;

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.

New Contributor
Posts: 2

Re: error message when I use the "then" statement

I tried that, but I received error messages as well. Here is my code:

froc format;

value type;

SR=1

AR=2

run;

I then ram an analysis for the type variable, but SAS did not recognize the variable type.

 

Super User
Posts: 10,516

Re: error message when I use the "then" statement

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

 

SR=1

AR=2

 

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.

Ask a Question
Discussion stats
  • 4 replies
  • 243 views
  • 0 likes
  • 3 in conversation