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;

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