Help using Base SAS procedures

Invalid Name when renaming variable in datastep

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 143
Accepted Solution

Invalid Name when renaming variable in datastep

Data Inventory;                            
  Set Dors_Mtg_Monthly_&Month_Minus_0.                    
    (keep = ln_no d_portfolio iv_id iv_cat_cd lm_status_cd d_lm_workout_status lm_set_up_dt bkr_status_cd      
    d_lm_workout_type lm_template_nm d_lm_workout_class                
    where = (lm_status_cd = 'A'));                      
  Days_In_LM = intck('days',lm_set_up_dt,"&EndDate."d);                  
  If Days_In_LM <= 30 Then                        
    Day_Band = 'a 0-30 Days   ';                      
  Else If Days_In_LM <= 60   Then                        
    Day_Band = 'b 31-60 Days   ';                      
  Else If Days_In_LM <= 90   Then                        
    Day_Band = 'c 61-90 Days   ';                      
  Else If Days_In_LM <= 120 Then                      
    Day_Band = 'd 91-120 Days ';                      
  Else If Days_In_LM <= 150 Then                      
    Day_Band = 'e 121-150 Days ';                      
  Else If Days_In_LM <= 180 Then                      
    Day_Band = 'f 151-180 Days ';                      
  Else If Days_In_LM > 180  Then                        
    Day_Band = 'g 181 Plus Days';                      
  if bkr_status_cd = 'A' then                        
    BK_status = 'Act BK';                        
  else BK_status = 'Non BK';                        
Run;                              
                               
sample output                            
LN_NO Days_In_LM Day_Band BK_status                      
0000755850 26 a 0-30 Days Non BK                        
0000813774 166 f 151-180 Days Non BK                        
0001059013 95 d 91-120 Days Non BK                        
0001618131 22 a 0-30 Days Non BK                        
0001721380 20 a 0-30 Days Non BK                        
0002301380 110 d 91-120 Days Non BK                        
0003428588 82 c 61-90 Days Non BK                        
0004036448 63 c 61-90 Days Non BK                        
0004116703 98 d 91-120 Days Non BK                        
0004344529 50 b 31-60 Days Non BK                        
0004851473 69 c 61-90 Days Non BK                        
0005369905 5 a 0-30 Days Non BK                        
0005441282 63 c 61-90 Days Non BK                        
0005441647 89 c 61-90 Days Non BK                        
0005705017 131 e 121-150 Days Non BK                        
0005715362 204 g 181 Plus Days Non BK                        
                               
The issue is when I apply this piece of code below to rename 'g 181 Plus Days' n to g_181_Plus_Days I get Error 214-322 Variable name ‘g 181 Plus Days’n not valid
proc transpose data = Transposed_Days_In_LM_Not_BK                    
  out = Transposed_Days_In_LM_Not_BK                      
  (keep = BK_status Total 'g 181 Plus Days'n rename = ('g 181 Plus Days'n = g_181_Plus_Days));            
  by BK_status;                          
  id Day_Band;                          
  var _freq_;                          
run;                              

Accepted Solutions
Solution
‎06-16-2016 02:49 PM
Super User
Super User
Posts: 7,067

Re: Invalid Name when renaming variable in datastep

How PROC TRANSPOSE converts the ID values to valid names depends on the setting of the VALIDVARNAME option.

Try running this program with different values for the VALIDVARNAME setting.

data have ;
  length LN_NO $10 Days_In_LM 8 Day_Band $32 BK_status $10 ;
  infile cards dsd dlm='|';
  input ln_no -- bk_status;
cards;
0000755850|26|a 0-30 Days|Non BK
0000813774|166|f 151-180 Days|Non BK
0001059013|95|d 91-120 Days|Non BK
0001618131|22|a 0-30 Days|Non BK
0001721380|20|a 0-30 Days|Non BK
0002301380|110|d 91-120 Days|Non BK
0003428588|82|c 61-90 Days|Non BK
0004036448|63|c 61-90 Days|Non BK
0004116703|98|d 91-120 Days|Non BK
0004344529|50|b 31-60 Days|Non BK
0004851473|69|c 61-90 Days|Non BK
0005369905|5|a 0-30 Days|Non BK
0005441282|63|c 61-90 Days|Non BK
0005441647|89|c 61-90 Days|Non BK
0005705017|131|e 121-150 Days|Non BK
0005715362|204|g 181 Plus Days|Non BK
;;;;
proc freq data=have ;
  tables bk_status*Day_band / noprint out=freq ;
run;
%let optsave=%sysfunc(getoption(validvarname));
options validvarname=any;
proc transpose data = freq out=want ;
  by BK_status;
  id Day_Band;
  var count;
run;
proc contents data=want;
run;
options validvarname=&optsave;

View solution in original post


All Replies
Super User
Posts: 5,515

Re: Invalid Name when renaming variable in datastep

[ Edited ]

I believe it's normal for PROC TABULATE to "correct" variable names automatically when taking values from the ID statement.  Try replacing the data set options:

 

(keep = BK_status Total 'g 181 Plus Days'n rename = ('g 181 Plus Days'n = g_181_Plus_Days))

 

Try using just KEEP and see if it gets you what you are looking for:

 

(keep = BK_status Total  g: )

 

Good luck.

Super User
Super User
Posts: 7,977

Re: Invalid Name when renaming variable in datastep

Sorry, that post is unreadable.  Have you posted from Excel or something.  There are grid lines everywhere, code all over.  Paste plain text and use code blocks for code (the {i} and SAS symbol above).

Solution
‎06-16-2016 02:49 PM
Super User
Super User
Posts: 7,067

Re: Invalid Name when renaming variable in datastep

How PROC TRANSPOSE converts the ID values to valid names depends on the setting of the VALIDVARNAME option.

Try running this program with different values for the VALIDVARNAME setting.

data have ;
  length LN_NO $10 Days_In_LM 8 Day_Band $32 BK_status $10 ;
  infile cards dsd dlm='|';
  input ln_no -- bk_status;
cards;
0000755850|26|a 0-30 Days|Non BK
0000813774|166|f 151-180 Days|Non BK
0001059013|95|d 91-120 Days|Non BK
0001618131|22|a 0-30 Days|Non BK
0001721380|20|a 0-30 Days|Non BK
0002301380|110|d 91-120 Days|Non BK
0003428588|82|c 61-90 Days|Non BK
0004036448|63|c 61-90 Days|Non BK
0004116703|98|d 91-120 Days|Non BK
0004344529|50|b 31-60 Days|Non BK
0004851473|69|c 61-90 Days|Non BK
0005369905|5|a 0-30 Days|Non BK
0005441282|63|c 61-90 Days|Non BK
0005441647|89|c 61-90 Days|Non BK
0005705017|131|e 121-150 Days|Non BK
0005715362|204|g 181 Plus Days|Non BK
;;;;
proc freq data=have ;
  tables bk_status*Day_band / noprint out=freq ;
run;
%let optsave=%sysfunc(getoption(validvarname));
options validvarname=any;
proc transpose data = freq out=want ;
  by BK_status;
  id Day_Band;
  var count;
run;
proc contents data=want;
run;
options validvarname=&optsave;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 264 views
  • 0 likes
  • 4 in conversation