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;
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.
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.
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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.