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

I am trying to run the below code but the values in variable (binary_sales) are missing. Please refer the attached screenshot for stat_globalsales dataset.

 

 

 

data binary_globalsale;
set stat_globalsale;
if Sales = '-Average' then binary_Sales = 0;
if Sales = '+Average' then binary_Sales = 1;
run;
proc print data=binary_globalsale;
run;


shot.png
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

@atulsingh wrote:

The thing is, earlier i have formated the Sales column using proc format such that the values which were above 2600 were labelled as '+Average' and the values below 2600 were rated as '-Average'.

 

Now i want the above values as:

'-Average' should become = 0 and 

'+Average' should become = 1.

 

What to do?


You need to use the underlying unformatted values or convert them using PUT in your recoding statements.

 

Re: your subject line "problem with sas code". You have another one today? Please use descriptive subjects in the future. We answer these questions not just to help you but any future users who have similar problems and choose to search for answers. 

View solution in original post

4 REPLIES 4
ballardw
Super User

Please verify that your Sales variable is actually a string. A picture may be showing a formated value and we can't tell.

If they are character did you check to see if your Sales values have leading spaces?

There is also a possibilit of a different charcter than the + and - from your keyboard in a character variable that looks similar.

 

You might examine the results of

data examine;
   set stat_globalsale (keep=sales); 
   y = rank('-');
   x = rank(substr(sales,1,1));
run;

to see if the values of x and y match where sales should be '-Average'. If they don't then you have different leading character(s). If X=32 there's a leading space.

 

atulsingh
Obsidian | Level 7

The thing is, earlier i have formated the Sales column using proc format such that the values which were above 2600 were labelled as '+Average' and the values below 2600 were rated as '-Average'.

 

Now i want the above values as:

'-Average' should become = 0 and 

'+Average' should become = 1.

 

What to do?

Reeza
Super User

@atulsingh wrote:

The thing is, earlier i have formated the Sales column using proc format such that the values which were above 2600 were labelled as '+Average' and the values below 2600 were rated as '-Average'.

 

Now i want the above values as:

'-Average' should become = 0 and 

'+Average' should become = 1.

 

What to do?


You need to use the underlying unformatted values or convert them using PUT in your recoding statements.

 

Re: your subject line "problem with sas code". You have another one today? Please use descriptive subjects in the future. We answer these questions not just to help you but any future users who have similar problems and choose to search for answers. 

Astounding
PROC Star

Implied by Reeza's comment, this would be a way:

 

if Sales > 2600 then binary_Sales = 1;

else binary_sales = 0;

 

You should refer to the actual value in your variable, not the formatted value.

 

Also, be careful to properly assign BINARY_SALES when SALES is negative, zero, or missing, or exactly 2600.  You have to at least consider the possibilities.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 2532 views
  • 1 like
  • 4 in conversation