BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cr2
Calcite | Level 5 cr2
Calcite | Level 5

The VVALUEX function allows fetching the value of a variable determined at run-time, by passing as argument another variable, for each observation. 

 

For example, the following statement in a datstep:

 

NEW_FIELD = vvaluex(DYN_FIELD);

 

Would produce the following result:

DYN_FIELDABCNEW_FIELD
A1231
A5675
B9101110
C13141515

 

I would like to perform the inverse: set a value of a variable determined at run-time, ideally, a function such as this, callable from within a datastep:

 

call SETVALUEX(DYN_FIELD, SOURCE_FIELD);

 

 

DYN_FIELDABCSOURCE_FIELD
A1 . .1
A22
B33
C . .44
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Why not just use an array?

%let varlist=A B C ;
%let n=%sysfunc(countw(&varlist));
%let namelist="%sysfunc(tranwrd(&varlist,%str( )," "))";
data want ;
  set have ;
  array vars &varlist ;
  array varnames (&n) $32 _temporary_ (&namelist);
  vars(whichc(dyn_field,of varnames(*))=source_field;
run;

You could generate the needed macro variables from the data.

proc sql noprint;
select distinct dyn_field,quote(trim(dyn_field))
  into :varlist separated by ' '
     , :varnames separated by ' '
  from have
;
%let n=&sqlobs;
quit;

 

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

Why not just use an array?

%let varlist=A B C ;
%let n=%sysfunc(countw(&varlist));
%let namelist="%sysfunc(tranwrd(&varlist,%str( )," "))";
data want ;
  set have ;
  array vars &varlist ;
  array varnames (&n) $32 _temporary_ (&namelist);
  vars(whichc(dyn_field,of varnames(*))=source_field;
run;

You could generate the needed macro variables from the data.

proc sql noprint;
select distinct dyn_field,quote(trim(dyn_field))
  into :varlist separated by ' '
     , :varnames separated by ' '
  from have
;
%let n=&sqlobs;
quit;

 

cr2
Calcite | Level 5 cr2
Calcite | Level 5
Not as clean as one would want, but it works perfectly.

Thanks!
34reqrwe
Quartz | Level 8

I had never came across these functions before. they are really interesting thanks. 

 

I managed to get the desired result by adding the VVALUE function :

 

data HAVE;
	dyn_field='A';
	A = 1;
	B=.;
	C=.;
	OUTPUT;
	dyn_field='A';
	A = 2;
	B=.;
	C=.;
	OUTPUT;
	dyn_field='B';
	A = .;
	B=3;
	C=.;
	OUTPUT;
	dyn_field='C';
	A = .;
	B=.;
	C=4;
	OUTPUT;
RUN;

DATA WANT;
	SET HAVE;
	SOURCE_FIELD=STRIP(VVALUEX(STRIP(VVALUE(DYN_FIELD))));
RUN;

 

Ksharp
Super User

SAS/IML has such function, check CALL SETVAL() .

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 4 replies
  • 1022 views
  • 1 like
  • 4 in conversation