BookmarkSubscribeRSS Feed
Sean_OConnor
Obsidian | Level 7

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

6 REPLIES 6
novinosrin
Tourmaline | Level 20
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;
FreelanceReinh
Jade | Level 19

Please note that for strings, e.g., '104428' < '3'.

Sean_OConnor
Obsidian | Level 7

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.

PeterClemmensen
Tourmaline | Level 20

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?

ballardw
Super User

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

 

singhsahab
Lapis Lazuli | Level 10

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;

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

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 6 replies
  • 1380 views
  • 0 likes
  • 6 in conversation