Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Solutions
- /
- Data Management
- /
- Extracting a year from a variable (in an array) and creating sums

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 06-06-2016 12:00 PM
(1212 views)

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;

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

4 REPLIES 4

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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).

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you!

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

**SAS Innovate 2025** is scheduled for May 6-9 in Orlando, FL. Sign up to be **first to learn** about the agenda and registration!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.