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
Hi,
Just put quotes.
IF COMPANY = "COKE" THEN COKED=1; ELSE COKED = 0;
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.
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.
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;
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.
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.
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.