Hi this appears conceptually to be simple so hopefully it is.
I have a large dataset with many variables and many records structured as follows:
Data have; input unique_ID$ George Fred Jane Mary Many_more_name select_person$; cards; kpss 0 0.3 1 1 0.6 Fred ussu 0.7 1 1 0 1 Mary esop 1 0.2 0 0 0.2 Fred aoal 0 1 0.1 0.3 1 Jane ; run;
For each record, I wish multiply by 0.8 the value of the variable whose name is stored in "select_person" such that I obtain:
unique_ID George Fred Jane Mary Many_more_name select_person kpss 0 0.24 1 1 0.6 Fred ussu 0.7 1 1 0 1 Mary esop 1 0.16 0 0 0.2 Fred aoal 0 1 0.08 0.3 1 Jane
Apologies if this appears elsewhere I posted earlier but could not find my post.
Many thanks in advnce
Hi @SASsyMartin and welcome to the SAS Support Communities!
You could create an array of the numeric variables, store their names in another array (in uppercase to avoid case mismatches) and then determine the relevant array index by means of the WHICHC function:
data want(drop=_:);
set have;
array _a[*] George--Many_more_name;
array _v[999] $32 _temporary_;
if _n_=1 then do _i=1 to dim(_a);
_v[_i]=upcase(vname(_a[_i]));
end;
_w=whichc(upcase(select_person), of _v[*]);
_a[_w]=round(0.8*_a[_w],1e-10);
run;
Increase the dimension of array _v if it needs to accommodate more than 999 variable names. The code above assumes variable names following the standards of VALIDVARNAME=V7. Depending on the numeric values in array _a you may want to use a different rounding unit or maybe not apply the ROUND function at all (at the risk of introducing rounding errors due to numeric representation issues).
VVALUEX()
want = input(vvaluex(select_person), best12.)*0.8;
@SASsyMartin wrote:
Hi this appears conceptually to be simple so hopefully it is.
I have a large dataset with many variables and many records structured as follows:
Data have; input unique_ID$ George Fred Jane Mary Many_more_name select_person$; cards; kpss 0 0.3 1 1 0.6 Fred ussu 0.7 1 1 0 1 Mary esop 1 0.2 0 0 0.2 Fred aoal 0 1 0.1 0.3 1 Jane ; run;For each record, I wish multiply by 0.8 the value of the variable whose name is stored in "select_person" such that I obtain:
unique_ID George Fred Jane Mary Many_more_name select_person kpss 0 0.24 1 1 0.6 Fred ussu 0.7 1 1 0 1 Mary esop 1 0.16 0 0 0.2 Fred aoal 0 1 0.08 0.3 1 JaneApologies if this appears elsewhere I posted earlier but could not find my post.
Many thanks in advnce
Here is a simple datastep that does the job. Less sophisticated than @FreelanceReinh 's suggestion but it should provide a good starting point :
data want;
set have;
array x George -- Many_more_name;
do i = 1 to dim(x);
if upcase(vname(x{i})) = upcase(select_person) then do;
x{i} = 0.8 * x{i};
leave;
end;
end;
drop i;
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.