The SAS Output Delivery System and reporting techniques

IF THEN command

Reply
Occasional Contributor
Posts: 8

IF THEN command

Not sure what but anytime I types the following command it highlight in red

if weight=999 then weight=.;

run;

or

if weight=999 then weight=.; and hit run

I tired %if weight=999 %then weight=.

Any help please

Respected Advisor
Posts: 4,130

Re: IF THEN command

Nothing wrong with the statement as such.

Does below code work for you?

data _null_;

  set sashelp.class;

  if weight=999 then weight=.;

run;

There might be something wrong in your code before this statement. That's why you would need to post the full data step - or the procedure code if this is Proc Report.

And: Which client is this? EG? And which version?

SAS Super FREQ
Posts: 8,814

Re: IF THEN command

And, some procedures have a WEIGHT option or WEIGHT statement. So sometimes, the syntax checker will flag those as red, but the code will execute correctly. Without seeing ALL of your code, it is hard to speculate.

You should not confuse macro syntax %IF with data step "regular" IF.

cynthia

Occasional Contributor
Posts: 8

Re: IF THEN command

1)

proc data=DMC_data.cholesterol;

var weight;

run;

The MEANS Procedure

N      means            Std dev       minimum       maximum

200172.375000064.5029264109.0000000999.0000000

then I did

2)

if weight =999 then weight=.;

and run the above proc procedure and still 999 was not replaces and if was highlighted in red.

I then tried the suggested syntax

data _null_;

  set sashelp.class;

  if weight=999 then weight=.;

followed by the above proc syntax and still 999 wasn't replaced.

run;

3)

proc data=DMC_data.cholesterol;

var weight;

if weight=999 then weight=.;

run;

Respected Advisor
Posts: 4,130

Re: IF THEN command

You need to invest some time in skilling up and understand the SAS concept of a Data Step and a Procedure.

Also consider using the EG wizards as this will support you in creating valid SAS code.

To give you a start below a simple example how your code could look like:

data test;
  set DMC_data.cholesterol;
  if weight=999 then weight=.;
run;

proc means data=test;
  var weight;
run;

Super User
Super User
Posts: 6,842

Re: IF THEN command

2) A) Cannot work as you cannot use an IF statement without some context to tell what to operate on.

    B) Worked fine, but because you told it to throw away the output of the data step (data _null_Smiley Wink it will have no effect other than to test the syntax of your IF statement

3) You cannot use an IF statement in a PROC.  You could use a WHERE statement to eliminate the WEIGHT=999 records from the data. (where weight ne 999Smiley Wink

You probably need to add a step some where to recode your special missing values from valid numbers like 999 into SAS missing or special missing values.  I would recommend doing this in the first step that reads in your data from the source (if you cannot modify it in the source).  Perhaps you are used to using SPSS which has support for indicating that normal values are to be treated as missing.  SPSS also has an implied data step for every analysis procedure that support using things like IF statements and other code that can transform the data before it is passed to the analysis algorithm.

Valued Guide
Posts: 3,208

Re: IF THEN command

Tom
You are right to aim at some basics. It are the concepts to be reviewed: SAS 9.3 Programmer's Bookshelf

Some nasty things:

1/ Missings
For statisticians it it quite common and makes sense to have many of them with different meanings.

- The SQL concept is failing in this part, just having one value "missing".

- SAS SPSS Stata (and others) are solving that it in different ways but supporting that.

  SAS has the option to work with many types: SAS(R) 9.3 Statements: Reference (missing).

-  The 9 habit as used in surveys is something going back to the hollerith approach, just having the numbers 0-9 as options.

   That time has passed but habits are pertinacious. At the data-cleansing step this should solved, adjusted

an old proceedng http://www.nesug.org/proceedings/nesug01/ps/ps8009.pdf

And (stata) Stata FAQ: How can I recode missing values into different categories?

2/ Formats

Nothing is what is what is seems to be. The numeric (floating) technical handling is rather basic as some quirks can pop-up.

The rounding and difference with integers is a pitfall.  Dates Times and datetimes are challenging. Unicode for the next frontier.

The concept of formats is rather unique with SAS. It is the only one environment/language I have seen where this is a segregated part of the data and can be seperately maintained. This offers quite smart and simple approaches not well known. Base SAS(R) 9.3 Procedures Guide, Second Edition (proc format).  You could use MLF (Multi Labe Format) own proceduers (FCMP)

The switch in thinking: No need to adjust data generate additonal datasets, just apply different formats at analyzing.

Note:

Base SAS(R) 9.3 Procedures Guide, Second Edition (Proc report) This procedure has a compute block (it is exceptional)

---->-- ja karman --<-----
Ask a Question
Discussion stats
  • 6 replies
  • 527 views
  • 1 like
  • 5 in conversation