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.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: