| Date: | Mon, 10 Aug 2009 14:21:14 -0700 |
| Reply-To: | mckbill <Bill.McKirgan@GMAIL.COM> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | mckbill <Bill.McKirgan@GMAIL.COM> |
| Organization: | http://groups.google.com |
| Subject: | Re: char to date or numeric format |
|
| Content-Type: | text/plain; charset=ISO-8859-1 |
On Aug 10, 2:49 pm, rahul alawadhi <rahulalawa...@gmail.com> wrote:
> Hi
> I have a large dataset of 100 variables and in them some of the
> variables are date variables in char format.
>
> I need to convert those variables into date format or numeric format.
> i have many variable for the date in the character type and need to
> convert them as well . So i need to use arrays over there. the thing
> is i need to place the variable in their original place where it was
> in the dataset.
>
> i just choose some of the varibels for example.
>
> data a;
>
> *input val1 val2 dtoday test1 test2 dbirth seq ;
>
> format val1 2.;
> format val2 $5.;
> format dtoday $8.;
> format test1 1.;
> format test2 $6.;
> format dbirth $8.;
> format seq 2.;
> input val1 1-2 val2 4-8 dtoday 10-17 test1 19 test2 21-26 dbirth
> 28-35 seq 37-38 ;
> datalines;
> 11 aaaaa 21081996 1 kdhsah 16011943 61
> 12 bbbbb 12022008 2 dhhsdh 12051961 51
> ;
> run;
>
> data b ;
> set a ;
>
> array date{*} Dtoday DBirth ;
>
> do i = 1 to dim(date);
>
> dd = input(trim(left(substr(date{i},1,2))),2.);
> mm = input(trim(left(substr(date{i},2,2))),2.);
> yy = input(trim(left(substr(date{i},4,4))),4.);
>
> x = mdy(mm,dd,yy);
> date{i} = input(x,8.);
> drop dd mm yy ;
>
> end;
> drop i;
> run;
>
> the problem u can see is dtoday , dbirth are in char format and cannot
> change to (date or num format) .
> this is just a rough code .
> basically i am not able to change the attribute of dbirth dtoday
>
> date{i} = input(x,8.);
>
> Is there some one who can help me in solving the problem. i need to
> keep the same location of the variable in the dataset.
Rahul,
If I understand you correctly...
1. You wish to change character representation of date numbers
(formatted MMDDYYYY (month day year) into SAS-dates (number of days
since 1jan1960)
2. You would like to change the variables so that they have the same
name, and are in the same column-wise location in the resulting
dataset
and....3. You have a large number of variables and wish to automate
the process.
I can give you one example of how to do the first two steps, and
suggest a way to achieve the third objective:
In DATA B...use RETAIN to cast the column-wise order of the dataset
variables.
In the SET statement it is important to rename the character variables
as you cannot change them from character to numeric using the same
variable name. The same-named variables in the retain statement will
be numeric, and RETAIN used here will keep them from being put to the
end (right-most) columns of the new dataset. The calculation of the
new dates with MDY is clearly understood by you, but I don't know of a
way to automate it as it looks like you were trying to do. The PUT
statement is there to make the new and old varables appear in the SAS
Log (raw and formatted). Finally, the DROP statement drops the string
variables that used to represent the dates.
data b;
/* retain the desired order */
retain val1 val2 dtoday test1 test2 dbirth seq ;
/* rename on set prior to making new date/numeric variables */
set a (rename= (dbirth = dbirth_str dtoday = dtoday_str ));
dbirth=mdy(substr(dbirth_str,3,2),
substr(dbirth_str,1,2),
substr(dbirth_str,5,4)
);
dtoday=mdy( substr(dtoday_str,3,2),
substr(dtoday_str,1,2),
substr(dtoday_str,5,4)
);
/* view it in the log */
put dbirth_str dbirth @20 dbirth mmddyy10. /
dtoday_str dtoday @20 dtoday mmddyy10. //
;
/* drop the string variables */
drop dbirth_str
dtoday_str ;
run;
This could be automated using list processing methods. If you had a
list of all DATE variable names, then you could use part of this
example as a template that could be populated with appropriate retain
statement lists, rename lists, MDY statements and so forth.
With hundreds of variables it might be just as easy to use copy/paste/
modify.
If you go for list processing, consider the use of proc contents and
output lists of variable names to another dataset for additional
processing into the kinds of statements needed to do the job.
This is just one idea at a solution, and I hope you get a few others
to choose from.
Regards,
Bill McKirgan
|