BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
PrinceAde
Obsidian | Level 7
Hi.
Please I need help getting these data set.
There are two SAS data sets.
“Have” contains four variables t1-t4 and site. I'm trying to add these variables across each site into a new variable l1-l3 in the data set “want”.
How can I do this?


Have;
Site t1 t2 t3 t4
B 1 4 5 3
C 1 2 3 3
D 2 1 5 1
E 1 4 5 3




Want;

Site t1 t2 t3 t4 l1 l2 l3
B 1 4 5 3 5 10 13
C 1 2 3 3 3 6 9
D 2 1 5 1 3 8 9
E 1 4 5 3 5 10 13

The code I have is not giving the desired output;
data want;
set have;
by Site;
array t[4] t1-t4;
array l[3] l1-l3;
retain l1-l3;

if first.Site then do;
l1 = t[1];
l2 = t[2];
l3 = t[3];
end;
else do;
l1 + t[1];
l2 + t[2];
l3 + t[3];
end;
run;


1 ACCEPTED SOLUTION

Accepted Solutions
Amir
Ammonite | Level 13

Hi,

 

Based on the data provided, I removed the by processing as each site was unique:

 

data want2;
  set have;

  array t[4] t1-t4;
  array l[3] l1-l3;
  
  l1 = sum(t[1],t[2]);
  l2 = sum(l1,t[3]);
  l3 = sum(l2,t[4]);
run;

 

 

Thanks & kind regards,

Amir.

View solution in original post

7 REPLIES 7
PeterClemmensen
Super User

Try this

 

data have;
input Site $ t1 - t4;
datalines;
B 1 4 5 3
C 1 2 3 3
D 2 1 5 1
E 1 4 5 3
;

data want(drop = i);
   set have;
   by Site;
   array t[4] t1-t4;
   array l[3] l1-l3;

   l[1] = sum(t[1], t[2]);

   do i = 2 to dim(l);
      l[i] = sum(l[i-1], t[i+1]);
   end;
run;

 

Result:

 

Site t1 t2 t3 t4 l1 l2  l3
B    1  4  5  3  5  10  13
C    1  2  3  3  3  6   9
D    2  1  5  1  3  8   9
E    1  4  5  3  5  10  13
PrinceAde
Obsidian | Level 7

@PeterClemmensen 

Thank you very much. it works perfectly.

Amir
Ammonite | Level 13

Hi,

 

Based on the data provided, I removed the by processing as each site was unique:

 

data want2;
  set have;

  array t[4] t1-t4;
  array l[3] l1-l3;
  
  l1 = sum(t[1],t[2]);
  l2 = sum(l1,t[3]);
  l3 = sum(l2,t[4]);
run;

 

 

Thanks & kind regards,

Amir.

PeterClemmensen
Super User

@Amir , sure thing, I left it there since I took your own code as the base.

 

Glad you found your solution.

Amir
Ammonite | Level 13

Hi @PeterClemmensen,

 

Thanks. My post was a reply to the OP, which is perhaps who you thought I was!

 

 

Thanks  kind regards,

Amir.

PrinceAde
Obsidian | Level 7

@Amir This works perfectly with my data.

Thank you!

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 178 views
  • 0 likes
  • 3 in conversation