BookmarkSubscribeRSS Feed
SAScph
Fluorite | Level 6

Hi 

I have some issues with creating dummy variables for single date observations. The code i run gives the value 1 for every observations in the dataset when running:

 

*Setting new outlier;
if date='24Dec2016'
date='05Dec2013'
date='24Dec2017'
date='24Dec2014'
date='24Dec2015'
date='24Dec2019'
date='24Dec2018'
date='24Sep2012'
date='06Jul2015'
then doutlier1=1; else doutlier1=0;

run;

 

 

3 REPLIES 3
Kurt_Bremser
Super User

Your code "gives" nothing, because it is invalid syntax:

 75         if date='24Dec2016'
 76         date='05Dec2013'
            ____
            22
 77         date='24Dec2017'
            ____
            22
 78         date='24Dec2014'
            ____
            22
 79         date='24Dec2015'
            ____
            22
 80         date='24Dec2019'
            ____
            22
 81         date='24Dec2018'
            ____
            22
 82         date='24Sep2012'
            ____
            22
 83         date='06Jul2015'
            ____
            22
 ERROR 22-322: Syntaxfehler, erwartet wird eines der folgenden: !, !!, &, *, **, +, -, /, ;, <, <=, <>, =, >, ><, >=, AND, EQ, GE, 
               GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=.  
 
 84         then doutlier1=1; else doutlier1=0;

If you actually have code that runs without ERRORs, post that.

To check if a date is in a given set of dates, use the IN operator:

data test;
input date yymmdd10.;
format date yymmddd10.;
if date in ('01jan2020'd,'08apr2020'd)
then doutlier = 1;
else doutlier = 0;
datalines;
2020-01-01
2020-01-02
2020-04-08
2020-04-09
;
ballardw
Super User

Or perhaps

doutlier = (date in ('01jan2020'd,'08apr2020'd) );
PaigeMiller
Diamond | Level 26

Dummy variables in the sense that you have multiple columns containing only 1 or 0 in each column, depending on the date?


This can be done via PROC GLMMOD (and several other procedures, as explained here), so you don't have to invent code to do this.

 

I feel compelled to add that for most analyses in SAS, you don't need to create DUMMY variables yourself, or via some PROC. Most analyses can be performed via a CLASS statement, in which SAS creates the dummy variables behind the scenes, so you don't need to create dummy variables at all. So what are you planning to do after you obtain your dummy variables?

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 445 views
  • 2 likes
  • 4 in conversation