2
votes

I have a table in SAS where in one column, the date is stored (e.g. "2005/10"). How do I have to convert this to a SAS data format?

Among many other tries, I tried this code:

data test;

        format date YYMMS.;

        date = input(ObservationMonth, YYMMS.);

        put date=date9.;

run;
3
Welcome to Stack Overflow! Questions which can easily be answered by consulting documentation are generally considered low-quality. Can you point to a resource which does it incorrectly (and include the code)? What error message did you get when you tried it?jpaugh
I'm sorry jpaugh that this question can be answered by documentation, I did not find one that matches my problem! I just tried a lot of code of similar things, e.g. the one I included in the question with the edit.bat
@bat post the type and format that your variable currently has. You can find these by running proc contents.Reeza
@Reeza it says char of length 200bat
Thanks, bat! Adding a code sample really improves your chances of getting a useful answer. It not only helps us gauge your point-of-view and skill level, but it also shows us that you're willing to work through the problem on your own, as much as you can. (From a practical standpoint, that means an answer only has to correct, rather than fabricate, code.)jpaugh

3 Answers

1
votes

You could just use the anydtdte. informat.

data want;
format date yymms.;
text="2005/10";
date=input(text,anydtdte.);
put date;
run;

This informat detects most date formattings and converts the character-stored value to a SAS date.

0
votes

One way is to use substr() and mdy() to extract the date components:

data _null_;
  ObservationMonth ="2005/10";
  date =mdy(substr(ObservationMonth,6,2),1,substr(ObservationMonth,1,4));
  put date = date9.;
run;

Another option is to use the anydtdte informat (note that results may differ depending on your locale):

data _null_;
  ObservationMonth ="2005/10";
  date =input(ObservationMonth,anydtdte.);
  put date = date9.;
run;

Yet another option is to modify the input to enable use of the YYMMDDw. informat:

data _null_;
  ObservationMonth ="2005/10";
  date =input(ObservationMonth!!'/01', YYMMDD10.);
  put date = date9.;
run;
0
votes

you're fantastic, guys! Thank you so much, with a "set" statement it works fine!