SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dane77221
Fluorite | Level 6

image.png

so with out writing all columns names how can I insert a column only by naming the column to the right and left?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
hashman
Ammonite | Level 13

@dane77221:

 

I like the elegance of the solution @ChrisNZ has offered. But since you said you want to specify nothing but the names of the surrounding variables, I'd offer one where you need to name only the variable after which you want the insertion (and, of course, necessarily the name and other attributes of the variable you want to insert). Thus, if you originally have a data set:

data have ;                                             
  retain A "A" B "BB" C "CCC" E "EEEEE" F "FFFFFF" G 1 ;
  do G = 1 to 10 ;                                      
    output ;                                            
  end ;                                                 
run ;                                                   

and want to insert a variable D after variable C, you could code as follows:

%let v_aft =  C ;                                                        
%let v_ins =  D ;                                                        
%let v_len = $4 ; * if you want D as numeric, just code 8 instead of $4 ;
                                                                         
data _null_ ;                                                            
  do until (z) ;                                                         
    set sashelp.vcolumn end = z ;                                        
    where libname = "WORK" and memname = "HAVE" ;                        
    length v_list $ 32767 ;                                              
    v_list = catx (" ", v_list, name) ;                                  
    if name = "&v_aft" then v_list = catx (" ", v_list, "&v_ins") ;      
  end ;                                                                  
  call symputx ("v_list", v_list) ;                                      
run ;                                                                    
                                                                         
data want ;                                                              
  retain &v_list ;                                                       
  length &v_ins &v_len ;                                                 
  call missing (&v_ins) ;                                                
  set have ;                                                             
run ;                                                                    

Now if you look at the data set WANT, you'll see the variable D right between C and E. The piece producing V_LIST may look rather maladroit, yet in reality it exacts little overhead since it only reads the metadata.  

 

Paul D.

View solution in original post

4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

Consider you have the data as below , this is a untested code but hopefully should work

 

data have;
input alphabets $;
cards;
A
B
C
E
F
G
H
;

create a dummy dataset;
data dum;
input alphabets $;
cards;
A
B
C
D
E
F
G
H
;

data all;
merge have dum;
by alphabets ;
run;

proc transpose data=all;
var alphabets;
run;
Thanks,
Jag
dane77221
Fluorite | Level 6

thank you for you help but the issue is there are two many columns to list out all column names.

ChrisNZ
Tourmaline | Level 20

Like this?

data WANT;
  if 0 then set HAVE(keep=A--C);
  length D 8;
  if 0 then set HAVE(keep=E--G);
  more code..  set HAVE;  more code...
run;

 

 

hashman
Ammonite | Level 13

@dane77221:

 

I like the elegance of the solution @ChrisNZ has offered. But since you said you want to specify nothing but the names of the surrounding variables, I'd offer one where you need to name only the variable after which you want the insertion (and, of course, necessarily the name and other attributes of the variable you want to insert). Thus, if you originally have a data set:

data have ;                                             
  retain A "A" B "BB" C "CCC" E "EEEEE" F "FFFFFF" G 1 ;
  do G = 1 to 10 ;                                      
    output ;                                            
  end ;                                                 
run ;                                                   

and want to insert a variable D after variable C, you could code as follows:

%let v_aft =  C ;                                                        
%let v_ins =  D ;                                                        
%let v_len = $4 ; * if you want D as numeric, just code 8 instead of $4 ;
                                                                         
data _null_ ;                                                            
  do until (z) ;                                                         
    set sashelp.vcolumn end = z ;                                        
    where libname = "WORK" and memname = "HAVE" ;                        
    length v_list $ 32767 ;                                              
    v_list = catx (" ", v_list, name) ;                                  
    if name = "&v_aft" then v_list = catx (" ", v_list, "&v_ins") ;      
  end ;                                                                  
  call symputx ("v_list", v_list) ;                                      
run ;                                                                    
                                                                         
data want ;                                                              
  retain &v_list ;                                                       
  length &v_ins &v_len ;                                                 
  call missing (&v_ins) ;                                                
  set have ;                                                             
run ;                                                                    

Now if you look at the data set WANT, you'll see the variable D right between C and E. The piece producing V_LIST may look rather maladroit, yet in reality it exacts little overhead since it only reads the metadata.  

 

Paul D.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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