BookmarkSubscribeRSS Feed
mascham
Calcite | Level 5

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

 

4 REPLIES 4
Astounding
PROC Star

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.

ballardw
Super User

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.

mascham
Calcite | Level 5

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.

 

ballardw
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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