BookmarkSubscribeRSS Feed
elsfy
Quartz | Level 8

Hi, 

I created a fixed table named TableA with a variable named "category' in which i added character values such as : 
First row of the table : "T_02_product"
2nd row of the table : "T_05_first_product"
etc..

Then i sorted TableA by  this variable before updating it with another table that also contains this key variable.

The problem is that the proc sort (whether i choose ascending or descending order), sorts it in a way that the 2nd row appears before the first one (or another row) that changes the order i have created in my fixed TableA.

How can i freeze the key column order (i.e 'category') and still sort because i need to update this table with another table's values ? 
Thanks

4 REPLIES 4
ballardw
Super User

You are missing something else. The sort of the shown values will not place the "T_05" before "T_02";

 

data junk;
   length value $ 25;
   value = "T_02_product";output;
   value = "T_05_first_product"; output;
run;

proc sort data=junk;
   by value;
run;

proc print data=junk;
run;

You do not show at all how you sort or update, which you say happens AFTER you sort, but if it involves Proc SQL that very well changed the order. Include and ORDER BY clause in Proc SQL to control order.

 

Or provide a more complete example data and the code you used. The SORT is not the problem unless you have a custom format displaying that text OR specified the REVERSE option or used the By Descending option.

elsfy
Quartz | Level 8

The values were just an example but the most important thing is that the first row of my master table is not the first row of my final output. I would like to specify the same order in the final output.

Here is my code :


proc sort data=tableA ; by var1 ;
proc sort data=tableB; by var1 ;

data tableC ; update tableA tableB ;
by var1;
run ;

ballardw
Super User

@elsfy wrote:

The values were just an example but the most important thing is that the first row of my master table is not the first row of my final output. I would like to specify the same order in the final output.

Here is my code :


proc sort data=tableA ; by var1 ;
proc sort data=tableB; by var1 ;

data tableC ; update tableA tableB ;
by var1;
run ;


Show the LOG from running that code. There should not be any issue because of SORT. Copy the code from the log, include the code AND all messages and notes. On the forum open a text box using the </> icon above the message window and paste the entire text.

 

IF you do some manual step after sorting but before the Update then you can very well change the sort order and the issue is not the behavior of the sort but the user.

If you need to update based on two or more variables you can use them on the BY statements in the Proc Sort and the Data step with update.

 

PS: Examples that do not show the actual behavior of your data are essentially useless. Actual values that show the real behavior are needed to be realistic.

pink_poodle
Barite | Level 11
If you freeze the key column then you can only sort if there are multiple records per key and only within group keys. Otherwise, you will be breaking the records from the keys.

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!

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
  • 4 replies
  • 1255 views
  • 0 likes
  • 3 in conversation