BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mazouz
Calcite | Level 5

I have this table: "Just for the exemple because in the reel problem I have more colomns and rows" 

VARIABLE

Q1Q2Q3Q4Q5
Class1121413412341234
Class21463456
Class334448
Class42121245156

Class5

1222244545

 

I calculate variation between Q1 Q2 Q3 ...

VARIABLEQ2/Q1Q3/Q2Q4/Q3Q5/Q4
Class11,166666679,571428579,208955221
Class241,55,666666671,64705882
Class31,33333333112
Class4613,753,46666667
Class51,833333331,090909091,8751

 

I want to creat table with just  value of Q2 or Q3 or Q4 or Q5 when first or last variation Exceeding  threshold=2

like this:

VARIABLEFirstLastFirst_QLast_Q
Class11341234Q3Q4
Class2434Q2Q4
Class3-8-Q5
Class412156Q2Q5
Class5----

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
infile cards expandtabs truncover;
input VAR $ Q1	Q2	Q3	Q4	Q5;
cards;
Class1	12	14	134	1234	1234
Class2	1	4	6	34	56
Class3	3	4	4	4	8
Class4	2	12	12	45	156
Class5 12	22	24	45	45
;

data temp;
 set have;
 array x{*} q1-q5;
 do i=1 to dim(x)-1;
  if x{i+1}/x{i} ge 2 then do;
    vname=vname(x{i+1});
	value=x{i+1};
	found=1;
	output;
  end;
 end;
 if not found then output;
 keep var vname value;
run;
data temp1;
 set temp;
 by var notsorted;
if not missing(vname) then do;
 if first.var then do;var_name='First';name='First_Q';end;
 if last.var then do;var_name='Last '; name='Last_Q ';end;
 if not missing(var_name) then output;
end;
else do;
 var_name='First';name='First_Q';output;
 var_name='Last '; name='Last_Q ';output;
end;
run;
proc transpose data=temp1 out=temp2(drop=_name_);
by var notsorted;
var value;
id var_name;
run;

proc transpose data=temp1 out=temp3(drop=_name_);
by var notsorted;
var vname;
id name;
run;

data want;
 merge temp2 temp3;
 run;

View solution in original post

1 REPLY 1
Ksharp
Super User
data have;
infile cards expandtabs truncover;
input VAR $ Q1	Q2	Q3	Q4	Q5;
cards;
Class1	12	14	134	1234	1234
Class2	1	4	6	34	56
Class3	3	4	4	4	8
Class4	2	12	12	45	156
Class5 12	22	24	45	45
;

data temp;
 set have;
 array x{*} q1-q5;
 do i=1 to dim(x)-1;
  if x{i+1}/x{i} ge 2 then do;
    vname=vname(x{i+1});
	value=x{i+1};
	found=1;
	output;
  end;
 end;
 if not found then output;
 keep var vname value;
run;
data temp1;
 set temp;
 by var notsorted;
if not missing(vname) then do;
 if first.var then do;var_name='First';name='First_Q';end;
 if last.var then do;var_name='Last '; name='Last_Q ';end;
 if not missing(var_name) then output;
end;
else do;
 var_name='First';name='First_Q';output;
 var_name='Last '; name='Last_Q ';output;
end;
run;
proc transpose data=temp1 out=temp2(drop=_name_);
by var notsorted;
var value;
id var_name;
run;

proc transpose data=temp1 out=temp3(drop=_name_);
by var notsorted;
var vname;
id name;
run;

data want;
 merge temp2 temp3;
 run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 378 views
  • 0 likes
  • 2 in conversation