BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Amerie
Calcite | Level 5

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

19 REPLIES 19
PaigeMiller
Diamond | Level 26

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
Amerie
Calcite | Level 5

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;

ballardw
Super User

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?

Amerie
Calcite | Level 5
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;
Reeza
Super User

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;

 

Amerie
Calcite | Level 5

Thank you. You were correct.

art297
Opal | Level 21

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

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Amerie
Calcite | Level 5

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.

Reeza
Super User

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;

 

Astounding
PROC Star

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.

Reeza
Super User

@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. 

 

 

Astounding
PROC Star

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.

art297
Opal | Level 21

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

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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