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

 

   I have a dataset like below. I would like to take the highest PCT (e.g. 0.82) and use its corresponding var2 (31) and var3(35), to append to the bottom of this dataset until the PCT reach 2, but use the same var2 (31) and var3(35)value.,

 

I am new to SAS array. Anyone could help? Thank you so much

 

 

data test;

input pct var2 var3;

datalines;

0.8 1 11

0.81 2 22

0.82 31 35

;

run;

proc print data=test;

run;

 

 

 FINAL output should looks like

0.8     1 11

0.81   2 22

0.82 31 35

0.83 31 35

0.84 31 35

0.85 31 35

0.86 31 35

0.87 31 35

0.88 31 35

0.88 31 35

0.90 31 35

0.91 31 35

0.92 31 35

0.93 31 35

0.94 31 35

0.95 31 35

until

2      31 35

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Use END to identify the end of file and then loop to 2 by 0.01.

 


data want;
set test end=eof;
output;

if eof then do i=pct to 2 by 0.01;
    pct=i;
    output;
end;

drop i;
run;

View solution in original post

4 REPLIES 4
Reeza
Super User

Use END to identify the end of file and then loop to 2 by 0.01.

 


data want;
set test end=eof;
output;

if eof then do i=pct to 2 by 0.01;
    pct=i;
    output;
end;

drop i;
run;
Memphisuser
Calcite | Level 5
Thank you so much for quick response, Reeza. Your solution works perfectly for me.
FreelanceReinh
Jade | Level 19

I am really sorry to disagree with @Reeza:

 

Firstly, the intended "FINAL output" did not contain a duplicate of the last observation from dataset TEST (assuming that the "highest PCT" is always at the end).

 

Secondly, numeric representation issues should be taken into account. Otherwise, @Memphisuser could experience surprises when working with dataset WANT.

 

I mean surprises like this:

 

/* Just for abbreviation, generate the list 0.83 0.84 ... 2.00 in a macro variable */

data _null_;
length numlist $600;
do i=83 to 200;
  numlist=catx(' ', numlist, put(i/100,4.2));
end;
call symput('nlist', numlist);
run;

%put &nlist;


/* Select the 200-83+1=118 obs. with 0.83<=PCT<=2.00 */

data sel;
set want;
if pct in (&nlist);
run; /* I get only 26 observations! 
        (The number is platform-dependent, though.) */

 

Therefore, I would like to modify the solution as follows:

data want;
set test end=eof;
output;

if eof then do i=round(pct*100+1) to 200;
    pct=i/100;
    output;
end;

drop i;
run;

 

Reeza
Super User
You don't have to be sorry to disagree with me, I'm frequently wrong 😉

I agree with the duplicate, but that can easily be dealt with. Regarding the rounding issue, that's pretty much expected when dealing with decimal numbers.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 933 views
  • 2 likes
  • 3 in conversation