BookmarkSubscribeRSS Feed
niloya
Fluorite | Level 6

Hi

 

I just would like to divide x column to y column. and the output should be spread over the earlier than x value. 

The total spreading value in the desired output should equal x value. For example, x =16 it was divided to 4. So, in the output, there should be 4, 4,4,4 

Input

xy
04
04
04
04
04
164
04
04
04
04
04
04
164
04
04
04
04
04
04
164
04

 

xyDesired Output
04 
04 
044
044
044
1644
04 
04 
04 
044
044
044
1644
04 
04 
04 
044
044
044
1644
04 
5 REPLIES 5
mkeintz
PROC Star

This is a task where one approach is to "look ahead".   My question is, will the precipitating record always have X=16 and Y=4?  If so, then you can always look ahead by a constant number of records.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
niloya
Fluorite | Level 6

@mkeintz  numbers can change and would be different numbers. This is just a data to show I would like to have.  16 could be 12 , or 20 and 4 could be 2 or 3.

Shmuel
Garnet | Level 18

What would be the desired output if the division result is not an integer ?

 

I would:

1- add to the dataset a sequence number,

2- sort it by descending sequnce, 

3- make the division as long as X is not zero and retain X as saved_X and the dividend 

4- for any X=0 assign the dividend, subtract it from saved_X until saved_X le 0

 

data temp1;
 set have;
      seq = _N_;
run;
proc sort data=temp1; by descending seq; run;

data temp2(keep=seq x y div);
  set temp1;
        retain saved_x div;
        if x ne 0 then do;
saved_x = x; div = int(x / y); output; end; else do;
saved_x = saved_x - div; if saved_x > div then output; else do; div=saved_x; output; end; end; run; proc sort data=temp2 out=want(keep=x y div); by seq; run;

 

Yavuz
Quartz | Level 8
What do you expect to see if the results cross
For example;
X Y
0 4
12 4
16 4
is this combination possible ?
Ksharp
Super User
data have;
infile cards expandtabs;
input x	y ;
cards;
0	4
0	4
0	4
0	4
0	4
16	4
0	4
0	4
0	4
0	4
0	4
0	4
16	4
0	4
0	4
0	4
0	4
0	4
0	4
16	4
0	4
;
data have;
 set have;
 n+1;
 if x>0 and y>0 then z=x/y;
run;
data key;
 set have(where=(z is not missing))	;
 do k=n-3 to n;
  output;
 end;
 keep k z;
run;
data want;
 if _n_=1 then do;
  if 0 then set key;
  declare hash h(dataset:'key');
  h.definekey('k');
  h.definedata('z');
  h.definedone();
 end;
 set have;
 rc=h.find(key:n);
 drop rc k n;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 923 views
  • 0 likes
  • 5 in conversation