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;
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.
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.