DATA Step, Macro, Functions and more

SAS array help needed

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

SAS array help needed

 

   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

 


Accepted Solutions
Solution
‎12-03-2015 10:47 AM
Super User
Posts: 17,818

Re: SAS array help needed

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


All Replies
Solution
‎12-03-2015 10:47 AM
Super User
Posts: 17,818

Re: SAS array help needed

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;
New Contributor
Posts: 2

Re: SAS array help needed

Thank you so much for quick response, Reeza. Your solution works perfectly for me.
Trusted Advisor
Posts: 1,115

Re: SAS array help needed

[ Edited ]

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;

 

Super User
Posts: 17,818

Re: SAS array help needed

You don't have to be sorry to disagree with me, I'm frequently wrong Smiley Wink

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.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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