BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ae_w105
Calcite | Level 5

Hi,

My original table is like the following.

ae_w105_0-1590649199831.png

Question1:

Is it possible to sort the values in the columns COL1 to COL4 with the column names remain unchanged.

Also, the value that is associated with the respective column needs to re-arranged as well.

For example, B is initially COL3, and the associated value in VAL3 is 400.

B will be in COL2, so 400 should be in VAL2 as well.

The desired output is as follows:

ae_w105_1-1590649257956.png

 

Question2:

Within the same key, compare the values in col1-col4 with that of the previous row.

If the current row doesn't have values from previous row, create additional columns prev_col: that record those values. Similarly for the val: columns.

If the current row has new value not found from previous row, create additional columns new_col: that record those values. Similarly for the val: columns.

The desired output is as follows:

ae_w105_2-1590649473266.png

 

Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

You can try the call sort function as below and it should generate the expected result

 

call sort(col1,col2,col3,col4);
call sort(val1,val2,val3,val4);
Thanks,
Jag

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

DO NOT POST DATA IN PICTURES!

We are not hired to type data off the screen for testing. Post example in a data step with datalines. Also use descriptive subject lines, as you were advised in your "welcome" mail. Using just "SAS" in the SAS-oriented online community is not a sign of overwhelming intelligence, to say the least.

Ron_MacroMaven
Lapis Lazuli | Level 10

 

the various call routines return their value in their arguments, in this case the array.

 

this code is on page 5 of the paper cited below.

 

   array  _n(&n_obs);
do _i = 1 to &n_obs ;   * read column into row;
   set &data (keep   =  &name
              rename = (&name = _value))
       point = _i;
   _n(_i) = _value;
   end;

call sortn(of _n(*));

 

@inproceedings{Fehd-2015_mwsug-rf-04_read-column-into-row,
year=2015,booktitle=name.mwsug,author={Ronald J. Fehd},
title ={Reading a Column into a Row to Count N-levels,
Calculate Cardinality Ratio and Create Frequency and Summary Output In One Step},
note ={Rapid Fire, 10 pp.},
url={http://www.lexjansen.com/mwsug/2015/RF/MWSUG-2015-RF-04.pdf}}

 

Jagadishkatam
Amethyst | Level 16

You can try the call sort function as below and it should generate the expected result

 

call sort(col1,col2,col3,col4);
call sort(val1,val2,val3,val4);
Thanks,
Jag

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

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