DATA Step, Macro, Functions and more

Diff in Arrays

Reply
Contributor
Posts: 39

Diff in Arrays

hi how to find diiference for below data using arrays

need diff between qtr1-qtr1n.. so on..

 

Obs id qtr1 qtr2 qtr3 qtr4 Qtr1n Qtr2n Qtr3n Qtr4n i 1 2 3

1025010015020062.5125.0187.52505
1046010012018075.0125.0150.02255
10680130160220100.0162.5200.02755
PROC Star
Posts: 547

Re: Diff in Arrays

How do you want the differences presentes? In seperate columns? Smiley Happy

Contributor
Posts: 39

Re: Diff in Arrays

yes same as qtr1m qtr2m qtr3m qtr4m

Super User
Super User
Posts: 7,392

Re: Diff in Arrays

Post test data in the form of a datastep, we are not here for data entry!

 

Something like:

data want;
  set have;
  array qtr{4};
  array qtr2{4} qtr1n qtr2n qtr3n qtr4n;
  array res{4};
  do i=1 to 4;
    res{i}=qtr2{i}-qtr{i};
  end;
run;

Why have you called array elements qtrXn, that just makes your programming harder, put the number at the end.

Contributor
Posts: 39

Re: Diff in Arrays

 

below is my original data set;

first I need increasing qtr value to 25% later i wanted difference between both by using arrays. kould you help on this

id qtr1 qtr2 qtr3 qtr4;

102 50 100 150 200

104 60 100 120 180

106 80 130 160 220

 

Super User
Posts: 6,927

Re: Diff in Arrays

For those who have a hard time grasping the concept, this is how you post data in a data step:

data have;
input id qtr1 qtr2 qtr3 qtr4;
cards;
102 50 100 150 200
104 60 100 120 180
106 80 130 160 220
;
run;

Now all possible helpers only need a copy/paste and "run" to recreate your dataset. Be courteous to those who shall help you.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 39

Re: Diff in Arrays

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Hi
That's I've done it already. Now I'm wanted increase values by 25% and
later I wanted find difference between both by using Arrays

Regards
Ravi
Super User
Super User
Posts: 7,392

Re: Diff in Arrays

Something like (again, not typing in):

data want;
  set have;
  array qtr{4};
  array qtr2{4};
  array res{4};
  do i=1 to 4;
    qtr2{i}=1.25 * qtr{1};
    res{i}=qtr2{i}-qtr{i};
  end;
run;

However this doesn't make much sense as diff will always be 25% so why the need for the second set of variables at all, just do:

res{i}=(qtr{i} / 100) * 25;

Ask a Question
Discussion stats
  • 7 replies
  • 168 views
  • 0 likes
  • 4 in conversation