Folks,
I have the following dataset;
data have;
infile cards expandtabs;
input Var1 $ Var2 $ ;
cards;
0100 104428
2566 36186
3 507094
40156 2566
588 49
;
run;
I need to rearrange the strings so the lowest value is always in column1. We need to end up with something like this;
data want;
infile cards expandtabs;
input Var1 $ Var2 $ ;
cards;
0100 104428
2566 36186
3 507094
2566 40156
49 588
;
run;
Any insight welcome
data have;
infile cards expandtabs;
input Var1 $ Var2 $ ;
cards;
0100 104428
2566 36186
3 507094
40156 2566
588 49
;
run;
data want;
set have;
call sortc(var1, var2);
run;
Please note that for strings, e.g., '104428' < '3'.
While not in the example above the strings I'm working on will all be length 13 and alphanumeric. They always end with a letter.
It is always a good idea to post example data that resembles your actual data.
Now, when they always end with a letter, how do you determine what value is larger?
@PeterClemmensen wrote:
It is always a good idea to post example data that resembles your actual data.
Now, when they always end with a letter, how do you determine what value is larger?
For an example is 123N supposed to come before or after 123a?
Default character comparisons will have all the upper case come before any of the lower case. This may not be an issue if your letters are always all upper or all lower case. If they have mixed case then your order potentially may not be as desired.
data have;
infile cards expandtabs;
input Var1 $ Var2 $ ;
cards;
0100 104428
2566 36186
3 507094
40156 2566
588 49
;
run;
data want;
set have;
array arr1[*] _all_;
array arr2[*] _ALL_;
do i=1 to dim(arr1);
do j=1 to dim(arr2);
if arr1[i]>arr2[j] then var1=arr2[j];
if arr1[i]<arr2[j] then var2=arr2[j];
end;
end;
drop i j;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.