The following, except for the fourth line from the bottom of the code, does what I want. However, in the section:
do _i=1 to 4;
want((_n_-1)*4+_i)=scan("&vars.",_i);
end;
I'd like to assign the values of var1, var2, var3 and var4
What do I have to change/add/modify in order to obtain that result?
TIA,
Art
data have (drop=months);
format id best32.;
input date (ind1-ind4) ($) ;
informat date date9.;
format date yymon7.;
array other_variable(2);
do id=1 to 2;
other_variable(id)=id;
end;
other_variable1=1;
other_variable2=2;
do id=1 to 10;
date="01dec2010"d;
do months=1 to 12;
date=intnx('month',date,1);
if not(months eq 9 and mod(id,2)) then output;
end;
end;
cards;
01dec2010 A B C D
;
%let filein=have;
%let by_variable=id;
%let vars=ind1 ind2 ind3 ind4;
%let numvars=4;
%let id_label=date;
proc sql noprint;
select distinct 'ind1_'||put(date,yymon7.)||
' ind2_'||put(date,yymon7.)||
' ind3_'||put(date,yymon7.)||
' ind4_'||put(date,yymon7.),
date
into :varlist separated by " ",
:junk
from &filein. (obs=1000) /*must be big enough to capture all months*/
order by &id_label.
;
quit;
data want;
set &filein.;
by &by_variable.;
array want(*) $ &varlist.;
retain want;
if first.id then call missing(of want(*));
_n_=month(date);
do _i=1 to 4;
want((_n_-1)*4+_i)=scan("&vars.",_i);
end;
drop &id_label. _: &vars.;
if last.&by_variable. then output;
run;
Use VVALUEX? Note that this will actually get the formatted value of the variable. Of course this means that you &VARS list can include numeric and character variables.
want((_n_-1)*4+_i)=vvaluex(scan("&vars.",_i));
You need another ARRAY.
array want(*) $ &varlist.;
array have $ &vars ;
...
want((_n_-1)*4+_i)=have(_i) ;
Tom, I'll come back and mark your response as "correct" rather than just "helpful" if no one can come up with a way of doing what I'd really like to do. However, using an array definitely solved the problem.
Use VVALUEX? Note that this will actually get the formatted value of the variable. Of course this means that you &VARS list can include numeric and character variables.
want((_n_-1)*4+_i)=vvaluex(scan("&vars.",_i));
: That was definitely what I was looking for and, yes, it would allow combining character and numeric variables if I changed the program so that it wasn't forcing all of the results into a single array.
However, I'm not going to use it, after all, as it came with a high price. It doubled the cpu time required for the overall process.
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.