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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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