Hello Everyone,
I am Trying to Swap the values of first and last record using Temporary Arrays.
data list;
infile datalines dsd;
input Num @@;
datalines;
12, 35, 9, 56, 24
;
run;
%Macro swap();
proc sql noprint;
select Num into :Num1-
from list;
quit;
Data result;
set list;
Array t[1:&sqlobs.] _TEMPORARY_;
%do i=1 %to &sqlobs.;
t[&i.]=&&Num&i.;
%end;
%if 1 %then %do;
_i=t[1];
t[1]=t[5];
t[5]=_i;
%end;
run;
%mend swap;
%swap();
Can anyone please tell me how to store the Temporary Array values into a permanent Variable and get the final output as :- 24, 35, 9, 56, 12
Thanks.
Macros and temporary arrays? I don't think those are needed at all.
data list;
infile datalines dsd;
input Num @@;
datalines;
12, 35, 9, 56, 24
;
run;
proc transpose data=list out=list_t prefix=t;
var num;
run;
data want;
set list_t;
_i=t1;
t1=t5;
t5=_i;
drop _i;
run;
Macros and temporary arrays? I don't think those are needed at all.
data list;
infile datalines dsd;
input Num @@;
datalines;
12, 35, 9, 56, 24
;
run;
proc transpose data=list out=list_t prefix=t;
var num;
run;
data want;
set list_t;
_i=t1;
t1=t5;
t5=_i;
drop _i;
run;
If you have more than 2 obs. in the dataset, this one works too:
data list;
infile datalines dsd;
input Num @@;
datalines;
12, 35, 9, 56, 24
;
run;
proc print;
run;
data want;
do point=nobs,2 to nobs-1,1;
set list point=point nobs=nobs;
output;
end;
STOP;
run;
proc print;
run;
Bart
Array as a construct exist per observation. So typically any array based approach to working across record/observation boundaries is likely to be suspect. Consider as a minimum do you know before you start what the maximum number of records that you need to process is? You have to define the array to handle that.
The sql approach you are attempting would be a nightmare if you have one or more Id variables to designate groups that need similar processing.
For example this accomplishes what I think you expect for output (your description lacks some precision as to actual desired output)
data junk; set list (firstobs=5) list (firstobs=2 obs=4) list (firstobs=1 obs=1) ; run;
But this would be pretty obnoxious to modify for any sort of grouping variables.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.