Jul

25

I’m back with another SAS Tip of the Day. Like a lot of people, I work with dates very often.

You get the idea. Inconveniently, in this particular case, I received the data in an Excel file and when I uploaded the file all of the dates were in character format. Here is a simple fix.

  1. Create an array of the character dates. Takes one statement. Note that you need that $ to indicate character variables.
  2. Create an array of your numeric dates. Takes one statement. Leave OFF the $ to indicate these are NOT character variables.
  3. Use a DO loop to fix any data problems, read into the new numeric variable and subtract 21,916. This is the number of days difference between the reference date for SAS and for Excel. You can read more about that here.
  4. Not required but good practice , since I was not going to use the character date values, I dropped those from the data set as well as the j subscript variable.

data fixdata ;
set fix1;
array chardates {9} $ birthdate date_app date_assess date_eligible date_ipe date_closed                    
date_employ date_int_completed date_last_contact
;
array numdates {9} date_birth app_date assess_date eligible_date ipe_date closed_date employ_date
int_completed_date last_contact_date
;
** Change all date variables to exclude invalid dates ;
** And from Excel to SAS date format ;

do j = 1 to 9 ;
if chardates{j} = "0000-00-00" then numdates{j} = . ;
else numdates{j} = chardates{j} - 21916 ;
end;
drop j birthdate date_app date_assess date_eligible date_ipe date_closed date_employ            date_int_completed date_last_contact;

 


I live in opposite world. I blog on SAS and statistics for fun and make games for a living. Check out Making Camp Premium. Learn about Ojibwe culture, brush up your math skills, learn more English and have fun. All for under two bucks.

Making Camp scene with buffalo, deer and rabbit

 

Jul

24

Contrary to appearances, this is not an abandoned blog. I’ve been super-busy with 7 Generation Games, where we released two new games and a customized app for a client this month! At the same time, I’m in Santiago, Chile piloting games for our Spanish language brand, Strong Mind Studios, you can read some of my blog in Spanish here.

Santa Lucia

Santa Lucia, next door

I decided to get back to blogging with a SAS tip of the day. Today’s tip is about the _character_ array.

If you didn’t know, now you know: All character variables are in the _character_ array

Often, I want to do something to every character variable in a data set, for example, set all of the values to upper case, so “diabetes”, “Diabetes” and “DIABETES” are not counted as three, different disabilities. Because I hate to expend unnecessary effort, I don’t want to list the names of every character variable and I don’t want to count how many there are because I’ll probably count wrong and then end up with errors.

Here is an example using the _character_ array.

data fixdata ;
set fix1;
array fixchars {*} _character_ ;
** Change all character values to upper case ;
do i = 1 to dim(fixchars);
fixchars{i} = upcase(fixchars{i}) ;
end ;

Just use an ARRAY statement, give your array a name and in the {} instead of the number of elements put a *  which SAS interprets as “the number of variables in the array are however many character variables there happen to be.

You might think you’d have to use the $ to specify that the _character_ array consists of character variables, but that’s kind of overkill and you actually don’t. It will work either way.

In my DO statement, I use the DIM function which will return the dimension of the array. That is, DO I = 1 to DIM(array_name) will do the statements from the first variable to however many happen to be in the array.

As you might guess, the UPCASE function returns the value in all upper case.


Have a kid? Like kids? Feel like a kid yourself? Check out our new game, Making Camp Premium, because maturity is over-rated.

 

 

Blogroll

WP Themes