BookmarkSubscribeRSS Feed
Silver77
Calcite | Level 5

This is supposed to convert a number of hours exercised to high and low (1 and 0) but it is not working, what am I doing wrong? BTW this was the exact code given by my professor 

 

data classdata; 
set classdata;
if exercise>3.25 then highex=1;
else if exercise>=0 and exercise<3.25 then highex=0;
run;
3 REPLIES 3
ballardw
Super User

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the "</>" to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "</>" icon or attached as text to show exactly what you have and that we can test code against.

 

Note: Use of the code construct

data classdata;
   set classdata;

completely replaces the data set on the SET statement. You may have corrupted your data and may have to start from scratch depending on what you have done since the first time you created the Classdata data set.

Especially when testing you should create new data sets so you can recover easily from a logic problem.

If you had run test code prior creating a variable Highex then you may be seeing values from that.

Note: Your values have a "hole" at exactly equal to 3.25 as you are comparing "greater than 3.25" and "less than 3.25". But you do not have any place to handle values of 3.25 if they occur.

yabwon
Onyx | Level 15

The only thing I could add to @ballardw comment is: strange works for me...

code:

data classdata;
  exercise=.; output;
  exercise=-3; output;
  exercise=0; output;
  exercise=0.25; output;
  exercise=3.2; output;
  exercise=3.25; output;
  exercise=3.5; output;
run;

data classdata; 
set classdata; 
if exercise>3.25 then highex=1; 
else if exercise>=0 and exercise<3.25 then highex=0; 
run;

result:

yabwon_0-1712158556414.png

 

 

B-)

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Patrick
Opal | Level 21

@Silver77 You should always try and formulate if/then/else constructs exhaustive and exclusive.

Exhaustive: Covers all cases. There is always one condition that gets True.

Exclusive: No overlaps. Exactly one condition gets True.

 

The syntax you shared is not exhaustive. You don't cover cases where Excercise is 3.25, missing or below zero.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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