Giter VIP home page Giter VIP logo

utl-rearrange-two-columns-so-that-the-first-column-values-are-always-larger-than-second-column's Introduction

utl-rearrange-two-columns-so-that-the-first-column-values-are-always-larger-than-second-column

Re-arrange two columns so that the first column values are always larger than second column
Re-arrange two columns so that the first column values are always larger than second column

github                                                                                                                                      
https://tinyurl.com/ycsuufmn                                                                                                                
https://github.com/rogerjdeangelis/utl-rearrange-two-columns-so-that-the-first-column-values-are-always-larger-than-second-column           
                                                                                                                                            
stackoverflow                                                                                                                               
https://tinyurl.com/y9q4bbt6                                                                                                                
https://stackoverflow.com/questions/62090306/rearrange-two-columns-so-that-the-first-column-values-are-always-larger-in-r                   
                                                                                                                                            
                                                                                                                                            
* Originally v2 was always larger than v1 but the data got scrambled.                                                                       
* I need to rearange the colums so the v2 is always larger than v1 and use all the data.                                                    
* I need to put the back into the correct order.                                                                                            
                                                                                                                                            
 Two solutions                                                                                                                              
       a. sas                                                                                                                               
       b. R                                                                                                                                 
          R can sort multiple columns in place                                                                                              
                                                                                                                                            
options validvarname=upcase;                                                                                                                
libname sd1 "d:/sd1";                                                                                                                       
data sd1.have;                                                                                                                              
  input id  V1 V2;                                                                                                                          
cards4;                                                                                                                                     
 36  63                                                                                                                                     
 01  73                                                                                                                                     
 96  59                                                                                                                                     
 22  42                                                                                                                                     
203 185                                                                                                                                     
127 197                                                                                                                                     
183  60                                                                                                                                     
159 200                                                                                                                                     
 52 128                                                                                                                                     
 63  27                                                                                                                                     
 19  02                                                                                                                                     
 67 178                                                                                                                                     
 14 174                                                                                                                                     
171  99                                                                                                                                     
 52 168                                                                                                                                     
195 121                                                                                                                                     
199  24                                                                                                                                     
118 153                                                                                                                                     
166  24                                                                                                                                     
149 209                                                                                                                                     
;;;;                                                                                                                                        
run;quit;                                                                                                                                   
                                                                                                                                            
*            _               _                                                                                                              
  ___  _   _| |_ _ __  _   _| |_                                                                                                            
 / _ \| | | | __| '_ \| | | | __|                                                                                                           
| (_) | |_| | |_| |_) | |_| | |_                                                                                                            
 \___/ \__,_|\__| .__/ \__,_|\__|                                                                                                           
                |_|                                                                                                                         
;                                                                                                                                           
                                                                                                                                            
                                                                                                                                            
WORK.WANT_SAS total obs=20 (same output SAs and R)                                                                                          
             | RULES                                                                                                                        
  V2    V1   |                                                                                                                              
             |                                                                                                                              
   2     1   |  V2 is always greater than V1                                                                                                
  24    14   |  This was the original order?                                                                                                
  24    19   |  Originally V2 inclued a stck dividend                                                                                       
  27    22   |                                                                                                                              
  42    36   |                                                                                                                              
  59    52   |                                                                                                                              
  60    52   |                                                                                                                              
  63    63   |                                                                                                                              
  73    67   |                                                                                                                              
  99    96   |                                                                                                                              
 121   118   |                                                                                                                              
 128   127   |                                                                                                                              
 153   149   |                                                                                                                              
 168   159   |                                                                                                                              
 174   166   |                                                                                                                              
 178   171   |                                                                                                                              
                                                                                                                                            
 185   183   |                                                                                                                              
 197   195   |                                                                                                                              
 200   199   |                                                                                                                              
 209   203   |                                                                                                                              
                                                                                                                                            
*          _       _   _                                                                                                                    
 ___  ___ | |_   _| |_(_) ___  _ __  ___                                                                                                    
/ __|/ _ \| | | | | __| |/ _ \| '_ \/ __|                                                                                                   
\__ \ (_) | | |_| | |_| | (_) | | | \__ \                                                                                                   
|___/\___/|_|\__,_|\__|_|\___/|_| |_|___/                                                                                                   
                                                                                                                                            
  __ _     ___  __ _ ___                                                                                                                    
 / _` |   / __|/ _` / __|                                                                                                                   
| (_| |_  \__ \ (_| \__ \                                                                                                                   
 \__,_(_) |___/\__,_|___/                                                                                                                   
                                                                                                                                            
;                                                                                                                                           
                                                                                                                                            
proc sort data=sd1.have out=havRan1(keep=v1);                                                                                               
  by v1;                                                                                                                                    
run;quit;                                                                                                                                   
                                                                                                                                            
proc sort data=sd1.have out=havRan2(keep=v2);                                                                                               
  by v2;                                                                                                                                    
run;quit;                                                                                                                                   
                                                                                                                                            
data want_sas;                                                                                                                              
  merge havRan1 havRan2;                                                                                                                    
run;quit;                                                                                                                                   
                                                                                                                                            
                                                                                                                                            
*_        ____                                                                                                                              
| |__    |  _ \                                                                                                                             
| '_ \   | |_) |                                                                                                                            
| |_) |  |  _ <                                                                                                                             
|_.__(_) |_| \_\                                                                                                                            
                                                                                                                                            
;                                                                                                                                           
                                                                                                                                            
* R can sort mutiple columns in place;                                                                                                      
                                                                                                                                            
%utl_submit_r64('                                                                                                                           
library(haven);                                                                                                                             
library(SASxport);                                                                                                                          
want<-read_sas("d:/sd1/have.sas7bdat");                                                                                                     
want[] <- apply(want, 2, sort);                                                                                                             
write.xport(want,file="d:/xpt/want.xpt");                                                                                                   
');                                                                                                                                         
                                                                                                                                            
libname xpt xport "d:/xpt/want.xpt";                                                                                                        
data want;                                                                                                                                  
  set xpt.want;                                                                                                                             
run;quit;                                                                                                                                   
libname xpt clear;                                                                                                                          

utl-rearrange-two-columns-so-that-the-first-column-values-are-always-larger-than-second-column's People

Contributors

rogerjdeangelis avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.