BookmarkSubscribeRSS Feed
adisal
Fluorite | Level 6

Hello , 

 

I am trying to calculate a value where I have to pick the value of A of the current rank and divide it with column B value of previous rank i.e. to calculate value of Rank 2 (Column C) = 164/13610. 

The calculation has to be done in proc sql. 

 

RankABC
114813610 
216414829164/13610
321919566219/19566
424724494247/19566
547526909475/24494
657631694576/26909

 

Please help how to do the calculation for each rank . Thanks.

3 REPLIES 3
andreas_lds
Jade | Level 19

It has been explained numerous times in this week alone, that calculation requiring row-order can't be done with proc sql - at least not with easy to read/maintain code. So here is a data-step solving your problem:

data want;
  set have;
  _b = lag(b);
 
  if _n_ > 1 then do;
    c = a / _b;
  end;

drop _b; /* EDIT */ run;
jimbarbour
Meteorite | Level 14

If the ranks in the first column, "Rank", are always sequential and always in order, then you could code the below.  I'm joining the table to itself.  

 

If the ranks are not always sequential, then a column would have to be added that would start with one and then increment by one until the end of the table.

 

Jim

 

DATA	Unranked_Data;
	INFILE	DATALINES	DSD	DLM=','	MISSOVER;

	INPUT	Rank	
			A	
			B
			;

DATALINES;
1,	148,	13610,	 
2,	164,	14829,	13610
3,	219,	19566,	14829
4,	247,	24494,	19566
5,	475,	26909,	24494
6,	576,	31694,	26909
;
RUN;

PROC	SQL	NOPRINT;
	CREATE	TABLE	Ranked_Data	AS
		SELECT	Rank.Rank
				,Rank.A
				,Rank.B
				,(Rank.A/Prev.B)	AS	C	FORMAT	12.6
			FROM			Unranked_Data	Rank
			LEFT	JOIN	Unranked_Data	Prev
				ON	Rank.Rank	=	Prev.Rank +  1
				;
QUIT;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 3231 views
  • 2 likes
  • 4 in conversation