BookmarkSubscribeRSS Feed
Sean_OConnor
Fluorite | Level 6

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
Fluorite | Level 6

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 951 views
  • 0 likes
  • 6 in conversation