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
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;
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;
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;
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.
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.