BookmarkSubscribeRSS Feed
aalasady0
Calcite | Level 5

Hello,

 

I'm trying to create dummy variables for companies in my data set. Unfortunately the output is all zeros.

 

This is my code below:

 

DATA WORK.IMPORT;
SET WORK.IMPORT;
IF COMPANY = COKE THEN COKED=1; ELSE COKED = 0;
IF COMPANY = KFC THEN KFCD=1; ELSE KFCD = 0;
IF COMPANY = PEPSI THEN PEPSID=1; ELSE PEPSID = 0;
IF COMPANY = NIKE THEN NIKED=1; ELSE NIKED = 0;
IF COMPANY = STARBUCKS THEN STARBUCKSD=1; ELSE STARBUCKSD = 0;
IF COMPANY = MCDONALD THEN MCDONALDD=1; ELSE MCDONALDD = 0;
IF COMPANY = HARDEES THEN HARDEESD=1; ELSE HARDEESD = 0;
RUN;

 

unfortunately the ouput for the dummy variables is all 0, so if the company is coke the output is also 0 and not 1

 

I don't understand what is wrong, please help.

 

Thanks

5 REPLIES 5
stat_sas
Ammonite | Level 13

Hi,

 

Just put quotes.

 

IF COMPANY = "COKE" THEN COKED=1; ELSE COKED = 0;

ChrisBrooks
Ammonite | Level 13

What @stat_sas says - basically what is happening in your code is that SAS assumes that COKE, NIKE etc are variables in the data set but once you add double quotes it takes them as string literals.

Loko
Barite | Level 11

Hi,

 

When you say COMPANY = COKE  actually you compare 2 variables - COMPANY and COKE. If the variablke COKE is missing in your dataset you will notice a NOTE within the log : Variable COKE is uninitialized.

 

The solution has already been provided by @stat_sas.

 

 

Astounding
PROC Star

Also note that once you add quotes, the exact spelling matters.  "COKE" is different than "Coke".  You may want to change the comparison:

 

if upcase(company) = "COKE" then CokeD=1; else CokeD=0;

 

ballardw
Super User

It might also help to describe how you are intending to use those dummy variables.

A large number of the SAS procedures will create internal dummies when needed so additional variables are not needed.

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 5 replies
  • 3536 views
  • 0 likes
  • 6 in conversation