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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 1802 views
  • 1 like
  • 4 in conversation