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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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