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

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.

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;
	

 

 

 

 

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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;
	

 

 

 

 

--
Paige Miller
yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



ballardw
Super User

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1338 views
  • 3 likes
  • 4 in conversation