DATA Step, Macro, Functions and more

set new variable to missing if time vaue in a character string is missing

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 142
Accepted Solution

set new variable to missing if time vaue in a character string is missing

Hi!

 

I have a messy time date variable in a character string.

 

the orignal values look like this...

 

Inc
291900MAR07
142300APR07
03APR07
10APR07

 

I'm creating two  separate variables (date_var time_var) from the original variable (inc)  In the values with the longer string the first two numbers are actually the day, and the 3-6 is the time.  Many of the values do not have a time entered.  What I want to do is have the time variables set to missing if the time is missing.

 

So, to create the date_var I did this (and it worked fine)


data want ;
set have (keep=rescuereportid inc);
inc=compress(inc);

date_var=substr(inc,1,2)||substr(inc,max(1,length(inc)-4));
run;

 

How can I create a time variable with variables that are different in length (i don't want to write over values that have existing time vlaues)?  Want to look like this....

 

Orig_Var Date_Var Time_Var
291900MAR07 29MAR07 1900
142300APR07 14APR07 2300
03APR07 03APR07 .
10APR07 10APR07 .

 

Help is appreciated!

 

 

 
 
 
 
 

Accepted Solutions
Solution
‎08-01-2017 03:49 PM
Super User
Posts: 19,869

Re: set new variable to missing if time vaue in a character string is missing

Use the LENGTH function to check the length and then conditionally process the variables.

 

if length(var) = 10 then do;
time= <your code>;
date=<your code>;
end;
else if length(var)=9 then do;
time=.;
date=.input(.... );
end;

View solution in original post


All Replies
Solution
‎08-01-2017 03:49 PM
Super User
Posts: 19,869

Re: set new variable to missing if time vaue in a character string is missing

Use the LENGTH function to check the length and then conditionally process the variables.

 

if length(var) = 10 then do;
time= <your code>;
date=<your code>;
end;
else if length(var)=9 then do;
time=.;
date=.input(.... );
end;
Regular Contributor
Regular Contributor
Posts: 162

Re: set new variable to missing if time vaue in a character string is missing

That exactly what I was going to suggest. Pout. Lol.
Super User
Posts: 19,869

Re: set new variable to missing if time vaue in a character string is missing


HB wrote:
That exactly what I was going to suggest. Pout. Lol.

@HB yeah...I should really log off and go work Smiley Happy

Frequent Contributor
Posts: 142

Re: set new variable to missing if time vaue in a character string is missing

@Reeza NO! don't log off and work! Then I wouldn't be able to work!! lol
Super User
Posts: 19,869

Re: set new variable to missing if time vaue in a character string is missing

@jenim514 aren't you in Ottawa, you only have a little bit left.

Frequent Contributor
Posts: 142

Re: set new variable to missing if time vaue in a character string is missing

@Reeza No, in San Diego... only half way through the day!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 131 views
  • 5 likes
  • 3 in conversation