BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mmea
Quartz | Level 8

Hi

 

In my datastep I want to make a new variable based on a date variable

Nor a specific date, men a date value.

 

If variable1 HAS a date then new_variable = 1

If it doesnt have a date the new variable = 0

 

How can I do so?

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

The logic here is: If variable1 can meaningfully be represented as a date, set new_varaible to 1. Else set new_variable to 0.

 

data have;
input variable1 $12.;
datalines;
01/01/2021  
nonsense    
32/01/2021  
somechar    
31/01/2021  
nonsense    
01jan2021   
somechar    
;

data want;
   set have;
   new_variable = input(variable1, anydtdte12.) > .;
run;

 

Result:

 

variable1   new_variable 
01/01/2021  1
nonsense    0 
32/01/2021  0 
somechar    0 
31/01/2021  1 
nonsense    0 
01jan2021   1 
somechar    0 

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

What do you mean by "If variable1 HAS a date" ?

 

I can guess that variable1 is a character variable? So, if the value in variable1 can represent a date, then set new_variable = 1.

 

Is that right? Can you post come examples of variable1?

mmea
Quartz | Level 8

the variable1 has the dates "2020-12-29" etc.

But can also contain "NULL"

 

In a data step I want to make a new variable (new_variable) that has the values 1 if variable 1 has a date or 0 if variable1 has no date

make sense?

PeterClemmensen
Tourmaline | Level 20

The logic here is: If variable1 can meaningfully be represented as a date, set new_varaible to 1. Else set new_variable to 0.

 

data have;
input variable1 $12.;
datalines;
01/01/2021  
nonsense    
32/01/2021  
somechar    
31/01/2021  
nonsense    
01jan2021   
somechar    
;

data want;
   set have;
   new_variable = input(variable1, anydtdte12.) > .;
run;

 

Result:

 

variable1   new_variable 
01/01/2021  1
nonsense    0 
32/01/2021  0 
somechar    0 
31/01/2021  1 
nonsense    0 
01jan2021   1 
somechar    0 
mmea
Quartz | Level 8

Thanks!

Can this be done in proc sql? or does it not know these times of functions?

PeterClemmensen
Tourmaline | Level 20

Proc SQL is no problem

 

data have;
input variable1 $12.;
datalines;
01/01/2021  
nonsense    
32/01/2021  
somechar    
31/01/2021  
nonsense    
01jan2021   
somechar    
;

proc sql;
   select variable1,
          (input(variable1, anydtdte12.) > .) as new_variable
   from have;
quit;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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