BookmarkSubscribeRSS Feed
scb
Obsidian | Level 7 scb
Obsidian | Level 7

May I know how to add a prefix to the variables below, e.g. x.book, x.car, x.ship, x.ball? The number of variables is not fixed.

 

data sample;
input var1 $ var2 $ var3 $ var4 $;
cards;
book car ship ball
;
run;

 

 

4 REPLIES 4
Kurt_Bremser
Super User

Valid SAS names cannot contain dots, only letters, digits and underlines.

 

The dot notation is reserved for table aliases in SQL and hash objects in the data step.

hashman
Ammonite | Level 13

@Kurt_Bremser:

 

With one exception: VALIDVARNAME=ANY. E.g., in this case:

 

option validvarname=any ;                              
                                                       
data sample ;                                          
  input (var1-var4) ($) ;                              
  cards;                                               
  book car ship ball                                   
  ;                                                    
run ;                                                  
                                                       
data _null_ ;                                          
  set sample (rename=(var1-var4="x.var1"n-"x.var4"n)) ;
  put _all_ ;                                          
run ;                                                  

The SAS log will say:

x.var1=book x.var2=car x.var3=ship x.var4=ball _ERROR_=0 _N_=1

Though methinks the poster of the question actually meant to prefix the values of the variables, not their names. Unfortunately, it's one of those situations where the intent can hardly be discerned, and the posted needs to read your opus "How to post code".

 

Best

Paul D. 

Kurt_Bremser
Super User

If you are looking for an array, it is defined and used like this:

data want;
set sample;
length test $100;
array vars {*} var1-var4;
do i = 1 to dim(vars);
  test = catx(',',test,vars{i});
end;
drop i;
run;
gamotte
Rhodochrosite | Level 12

Hello,

 

data sample;
drop i;

input var1 $ var2 $ var3 $ var4 $;

array vars(*) var:;
array newvars(*) $10. newvar1-newvar4;

do i=1 to dim(vars);
    newvars(i)=cats('x.',vars(i));
end;
cards;
book car ship ball
;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 4559 views
  • 2 likes
  • 4 in conversation