## Extracting a year from a variable (in an array) and creating sums

Solved
Occasional Contributor
Posts: 8

# Extracting a year from a variable (in an array) and creating sums

I need help with the following code below. I do not think that my year{i} variable is formatted correctly and I cannot figure out why. When I attempt to create a sum for each year and type (type1sum09-12, etc.) I end up with a zero for each year. Any help would be greatly appreciated. Thanks.

data transpose3;
set transpose2;
array type[759];
array visitdate[759];
array yearx[759];
array newdate[759];

type1sum09 = 0;
type1sum10 = 0;
type1sum11 = 0;
type1sum12 = 0;

type2sum09 = 0;
type2sum10 = 0;
type2sum11 = 0;
type2sum12 = 0;

type3sum09 = 0;
type3sum10 = 0;
type3sum11 = 0;
type3sum12 = 0;

type1sum = 0;
type2sum=0;
type3sum=0;

/*summing the variables, by type, to get a per person count of each service*/
do i=1 to 758;

newdate{i}= put(visitdate{i},year4.);
yearx{i} = input (newdate{i},4.);

if type{i} = 1 and yearx{i} = 2009 then type1sum09 = type1sum09 + 1;
if type{i} = 1 and yearx{i} = 2010 then type1sum10 = type1sum10 + 1;
if type{i} = 1 and yearx{i} = 2011 then type1sum11 = type1sum11 + 1;
if type{i} = 1 and yearx{i} = 2012 then type1sum12 = type1sum12 + 1;

if type{i} = 2 and yearx{i} = 2009 then type2sum09 = type2sum09 + 1;
if type{i} = 2 and yearx{i} = 2010 then type2sum10 = type2sum10 + 1;
if type{i} = 2 and yearx{i} = 2011 then type2sum11 = type2sum11 + 1;
if type{i} = 2 and yearx{i} = 2012 then type2sum12 = type2sum12 + 1;

if type{i} = 3 and yearx{i} = 2009 then type3sum09 = type3sum09 + 1;
if type{i} = 3 and yearx{i} = 2010 then type3sum10 = type3sum10 + 1;
if type{i} = 3 and yearx{i} = 2011 then type3sum11 = type3sum11 + 1;
if type{i} = 3 and yearx{i} = 2012 then type3sum12 = type3sum12 + 1;

if type{i} = 1 then type1sum = type1sum+ 1;
if type{i} = 2 then type2sum = type2sum+ 1;
if type{i} = 3 then type3sum = type3sum+ 1;

end;
drop i;

keep uidnum type1-type759 type1sum type2sum type3sum type1sum09 type1sum10 type1sum11 type1sum12
type2sum09 type2sum10 type2sum11 type2sum12 type3sum09 type3sum10 type3sum11 type3sum12;

run;

Accepted Solutions
Solution
‎06-06-2016 04:17 PM
Super User
Posts: 6,634

## Re: Extracting a year from a variable (in an array) and creating sums

It looks like you're creating about 1,500 variables that you don't really need.  Consider replacing your DO loop with this:

do i=1 to 758;

year = year(visitdate{i});

if type{i}=1 then do;

type1sum+1;

if year=2009 then type1sum09 + 1;

else if year=2010 then type1sum10 + 1;

else if year=2011 then type1sum11 + 1;

else if year=2012 then type1sum12 + 1;

end;

else if type{i}=2 then do;

** similarly ...;

end;

else if type{i}=3 then do;

** similarly ...;

end;

drop i year;

end;

Not sure why the loop should go from 1 to 758 instead of 1 to 759, but that's how the code was posted originally.

All Replies
Super User
Posts: 13,321

## Re: Extracting a year from a variable (in an array) and creating sums

First would be to varify that your Visitdate variables are actually SAS date values (proc contents and look at the format or use the SAS/Explorer to examine variables)

After that since I don't see any explicit definition of the array elements in Newdate I suspect that the length of those elements has defaulted to \$ 8. Which when written with put(date,year4.) are Right justified in the field and then the Input(newdate,4.) reads the First 4 characters which are blank.

Unless you really need that newdate character variable then you may get by with

yearx[i] = year(visitdate[i]);

if you need the Newdate to hold exactly 4 characters then the array definition

array newdate(759) \$ 4;

may help.

Of course, I'm also not seeing where the other arrays get associated with variables from Transpose2 explicitly (which would be a very good idea).

Occasional Contributor
Posts: 8

## Re: Extracting a year from a variable (in an array) and creating sums

Thank you!

Solution
‎06-06-2016 04:17 PM
Super User
Posts: 6,634

## Re: Extracting a year from a variable (in an array) and creating sums

It looks like you're creating about 1,500 variables that you don't really need.  Consider replacing your DO loop with this:

do i=1 to 758;

year = year(visitdate{i});

if type{i}=1 then do;

type1sum+1;

if year=2009 then type1sum09 + 1;

else if year=2010 then type1sum10 + 1;

else if year=2011 then type1sum11 + 1;

else if year=2012 then type1sum12 + 1;

end;

else if type{i}=2 then do;

** similarly ...;

end;

else if type{i}=3 then do;

** similarly ...;

end;

drop i year;

end;

Not sure why the loop should go from 1 to 758 instead of 1 to 759, but that's how the code was posted originally.

Occasional Contributor
Posts: 8