BookmarkSubscribeRSS Feed
SASsyMartin
Calcite | Level 5

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

 

 

3 REPLIES 3
FreelanceReinh
Jade | Level 19

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

Reeza
Super User

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	        Jane

Apologies if this appears elsewhere I posted earlier but could not find my post.

Many thanks in advnce

 

 


 

PGStats
Opal | Level 21

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 1763 views
  • 3 likes
  • 4 in conversation