DATA Step, Macro, Functions and more

Re: If Else Error

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Re: If Else Error

Hi,

 

I am becoming reacquainted with SAS after a long hiatus and am having trouble with some code. I am trying to recode a variable using the if-else statement and am continuously getting an error message. I can't seem to figure out the issue. I am working with BRFSS data. Any suggestions on what the problem may be?  Thank you in advance!

 

if POORHLTH in (77,99,.) then ActsLimited=.;
	else if POORHLTH=88 then ActsLimited=0; *0 Days activities limited by poor health;
	else if 1 le POORHLTH le 13 then ActsLimited=1; *1-13 Days activities limited by poor health;
	else ActsLimited=2; *14 or more days activities limited due to poor health;
run;

Accepted Solutions
Solution
‎04-20-2018 12:25 PM
Super User
Posts: 23,295

Re: If Else Error

There's nothing inherently wrong with the code written. 

Post your log with  the full error.

 

For demonstration purposes, this shows that your code works as expected:

 

data demo;
input poorhlth;
cards;
77
99
.
88
1
2
3
4
5
6
9
13
14
0
24
23
100
;
run;


DATA Health2018; SET demo;

/*Poor Health*/
if POORHLTH in (77,99,.) then ActsLimited=.;
	else if POORHLTH=88 then ActsLimited=0; *0 Days activities limited by poor health;
	else if 1 le POORHLTH le 13 then ActsLimited=1; *1-13 Days activities limited by poor health;
	else ActsLimited=2; *14 or more days activities limited due to poor health;
run;

@Amerie wrote:
DATA Health2018; SET php1.finalvar;

/*Poor Health*/
if POORHLTH in (77,99,.) then ActsLimited=.;
	else if POORHLTH=88 then ActsLimited=0; *0 Days activities limited by poor health;
	else if 1 le POORHLTH le 13 then ActsLimited=1; *1-13 Days activities limited by poor health;
	else ActsLimited=2; *14 or more days activities limited due to poor health;
run;

 

View solution in original post


All Replies
Respected Advisor
Posts: 2,825

Re: If Else Error

You're getting an error?????

 

Would you be kind enough to share it with us? We want to see the SASLOG, or other indication of the error.

--
Paige Miller
Occasional Contributor
Posts: 6

Re: If Else Error

Posted in reply to PaigeMiller

I am! And cannot figure out why.

 

Here it is.

 

ERROR 180-322: Statement is not valid or it is used out of proper order.


59 if POORHLTH in (77,99,.) then ActsLimited=.;
--
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

60 else if POORHLTH=88 then ActsLimited=0;
----
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

61 else if 1 le POORHLTH le 13 then ActsLimited=1;
----
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

62 else ActsLimited=2;
----
180

ERROR 180-322: Statement is not valid or it is used out of proper order.

63 run;

Super User
Posts: 13,321

Re: If Else Error

Show the entire data step code.

And best is to paste into a code box opened with the forums {I} menu icon. The message windows will reformat text moving such things as the _ character SAS places under the location of the found error.

 

You did have this in a block of code that started with something like:

 

Data new;

   set old;

   if PoorHealth in (77,99,.) then ...

 

didn't you?

Occasional Contributor
Posts: 6

Re: If Else Error

DATA Health2018; SET php1.finalvar;

/*Poor Health*/
if POORHLTH in (77,99,.) then ActsLimited=.;
	else if POORHLTH=88 then ActsLimited=0; *0 Days activities limited by poor health;
	else if 1 le POORHLTH le 13 then ActsLimited=1; *1-13 Days activities limited by poor health;
	else ActsLimited=2; *14 or more days activities limited due to poor health;
run;
Solution
‎04-20-2018 12:25 PM
Super User
Posts: 23,295

Re: If Else Error

There's nothing inherently wrong with the code written. 

Post your log with  the full error.

 

For demonstration purposes, this shows that your code works as expected:

 

data demo;
input poorhlth;
cards;
77
99
.
88
1
2
3
4
5
6
9
13
14
0
24
23
100
;
run;


DATA Health2018; SET demo;

/*Poor Health*/
if POORHLTH in (77,99,.) then ActsLimited=.;
	else if POORHLTH=88 then ActsLimited=0; *0 Days activities limited by poor health;
	else if 1 le POORHLTH le 13 then ActsLimited=1; *1-13 Days activities limited by poor health;
	else ActsLimited=2; *14 or more days activities limited due to poor health;
run;

@Amerie wrote:
DATA Health2018; SET php1.finalvar;

/*Poor Health*/
if POORHLTH in (77,99,.) then ActsLimited=.;
	else if POORHLTH=88 then ActsLimited=0; *0 Days activities limited by poor health;
	else if 1 le POORHLTH le 13 then ActsLimited=1; *1-13 Days activities limited by poor health;
	else ActsLimited=2; *14 or more days activities limited due to poor health;
run;

 

Occasional Contributor
Posts: 6

Re: If Else Error

Thank you. You were correct.

PROC Star
Posts: 8,149

Re: If Else Error

You'd have to show you whole datastep. Your likely just missing a semi-colon or something on one of the lines preceding the code you showed.

 

Art, CEO, AnalystFinder.com

 

Super User
Super User
Posts: 9,416

Re: If Else Error

Data steps need to start with a data command and a dataset to output to.  Then they might have a set command or a merge command or other statements:

data <dataset>;
  set <dataset>;


or 
data <dataset>;
  merge <dataset1> <dataset2>;
  by <variables>;

The error is telling you a command if outside a datastep is not valid.

Occasional Contributor
Posts: 6

Re: If Else Error

Thank you. The code was correct. Something was going on outside of the datastep that I was unable to see. I rearranged and recoded it and it ran. Thank you again.

Super User
Posts: 23,295

Re: If Else Error

Since there's nothing wrong with the code shown the error is above the section indicated in your posts.

 


@Amerie wrote:

Hi,

 

I am becoming reacquainted with SAS after a long hiatus and am having trouble with some code. I am trying to recode a variable using the if-else statement and am continuously getting an error message. I can't seem to figure out the issue. I am working with BRFSS data. Any suggestions on what the problem may be?  Thank you in advance!

 

if POORHLTH in (77,99,.) then ActsLimited=.;
	else if POORHLTH=88 then ActsLimited=0; *0 Days activities limited by poor health;
	else if 1 le POORHLTH le 13 then ActsLimited=1; *1-13 Days activities limited by poor health;
	else ActsLimited=2; *14 or more days activities limited due to poor health;
run;


@Amerie wrote:

Hi,

 

I am becoming reacquainted with SAS after a long hiatus and am having trouble with some code. I am trying to recode a variable using the if-else statement and am continuously getting an error message. I can't seem to figure out the issue. I am working with BRFSS data. Any suggestions on what the problem may be?  Thank you in advance!

 

if POORHLTH in (77,99,.) then ActsLimited=.;
	else if POORHLTH=88 then ActsLimited=0; *0 Days activities limited by poor health;
	else if 1 le POORHLTH le 13 then ActsLimited=1; *1-13 Days activities limited by poor health;
	else ActsLimited=2; *14 or more days activities limited due to poor health;
run;

 

Super User
Posts: 6,632

Re: If Else Error

For the first error, the advice you have received is sound.  We would need to see the entire DATA step.

 

For most of the errors, the mistake is right there in your code.  The comment statements throw off the IF/THEN/ELSE logic.  You can't have a comment statement before an ELSE statement ... it removes the connection with the previous IF condition.  You could change all your comments statements:

 

* Not like this;

/* but like this instead */

 

Embedded comments won't disconnect the IF THEN from the subsequent ELSE IF.

 

But the first error is still a mystery, and depends on what came prior to that in the program.

Super User
Posts: 23,295

Re: If Else Error

Posted in reply to Astounding

@Astounding wrote:

{deleted}

 

For most of the errors, the mistake is right there in your code.  The comment statements throw off the IF/THEN/ELSE logic.  You can't have a comment statement before an ELSE statement ... it removes the connection with the previous IF condition.  You could change all your comments statements:

 

* Not like this;

/* but like this instead */

 

Embedded comments won't disconnect the IF THEN from the subsequent ELSE IF.

 

But the first error is still a mystery, and depends on what came prior to that in the program.


I don't think that's true, you can have comments within the IF/ELSE IF statements regardless of type. 

 

 

Super User
Posts: 6,632

Re: If Else Error

It's been probably 30 years since I tested this, but I doubt it has changed.  I'm just not in a position to test it until Monday.  Let us know what you find out.

PROC Star
Posts: 8,149

Re: If Else Error

[ Edited ]
Posted in reply to Astounding

I ran the code that @Reeza posted, which included the embedded *  ; comments, and it appeared to run perfectly.

 

In fact, her example data showed a flaw that should be fixed in @Amerie's code. The conditions don't trap values of 0, thus they're incorrectly recoded as 2 (i.e., representing GE 14)

 

Art, CEO, AnalystFinder.com

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 19 replies
  • 215 views
  • 0 likes
  • 8 in conversation