BookmarkSubscribeRSS Feed
mmaximos
Obsidian | Level 7

 

data mydata; 
	set sample;
    if month >= '04187.' then intervention1 = 1;
    else intervention1= 0;
run;

Hello all, 

 

 

I have a dataset with multiple intervention levels for which I am trying to run an interrupted time series analysis. I am trying to use 0 and 1 for each intervention to denote when the intervention started. I have the code for that listed above, but the selection for the if month >= '04187.' is not working. When I try to run this code, it's not that I get an error, but the intervention level I created is "1" throughout and not starting at the data April 2018. I've tried a whole bunch of variations for that date field but haven't been successful. 

 

This is how I have my date formatted below:

data mydata; 
	set sample;
    time = put(time_period,6.);
    month = input(time, YYMMn6.);
  format month monyy7.;
  run;

Any thoughts for how to address this?

 

Thank you

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Please look at your data set and then tell us some typical values of MONTH in data set SAMPLE.

 

Is there really a value of '04187.' in the data set SAMPLE with a period at the end? What does '04187.' with a period at the end mean? What are you trying to compare month to? Please explain this part clearly.

--
Paige Miller
ballardw
Super User

Month, if the code you show for the Mydata data step would be numeric in value. So you likely have conversion of character values such as in this example.

87   data junk;
88      month= input('202205',yymmn6.);
89      put month= best12. month= monyy.;
90      format month monyy.;
91      if month >= '04187.' then intervention1 = 1;
92       else intervention1= 0;
93   run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      91:16
month=22766 month=MAY22

IF your month is actually a date as implied by your input statement the values are being compared, in effect, to the the date of 27Jan1964. So if your generated date values are later than 27Jan1964 then the Intervention would obviously be 1. To compare DATE values to a specific date then use the form "ddMONyyyy"d  or '01Jan2022'd with the date in quotes followed by d. Nothing else is likely to convert properly.

 

You are asking for all sorts of conversion confusion if you let SAS decide which way to play with things when you compare character and numeric values. It is best to take control and provide the conversion your self.

 

I am a tad suspicious of your whole code for the Mydata data set as to if that step was done correctly.

 

Note: the only form of date I would expect to be referenced with 5 digits is a Julian date but your value is out of range to considered as such.

mmaximos
Obsidian | Level 7

Currently, the date field is numeric with a length of 8 and appears as such in the output as an example "201511". So as I understand, I need to convert that to a character value first and then to a SAS date. Is that correct? 

 

My date field in my dataset is named time_period, so this is how I went about converting the date field in the initial dataset into a SAS date:

data mydata; 
	set sample;
    sasdate=input(time_period, yymmn6.);
    format sasdate monyy.;
run;

However, that resulted in the sasdate column being "." all the way down (no distinct numbers from that Jan 1964 starting point). I think I am missing a step to convert that date field from numeric to character, but don't quite know how to fix this.

 

Thanks

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 468 views
  • 0 likes
  • 3 in conversation