From: chrismarx on
I know this question has been asked before, and I've read many of the
posts, but I still don't see how to make this work. I wrote the
following macro to recode data values:


%macro updateTableValue(dataset,varName,orgVals,newVarName,newVals);
data &dataset; set &dataset;
%let i = 1;
%do %while (%qscan(&orgVals,&i ) ne );
%let orgVal = %qscan(&orgVals,&i);
%let newVal = %qscan(&newVals,&i);
if &varName = &orgVal then &newVarName = &newVal;
%let i=%eval(&i+1);
%end;
run;
%mend;

%updateTableValue(rename_t1, My_family_or_friends_will_enjoy, 'Very
important' 'Somewhat important', GBBC_TEST2, 1 2)


I'ved tried other variations, but I'm hoping maybe someone can point
me in the right direction here. Is is advisable to pass in a list of
values that way i am currently, (ie list of quoted values, 'a' 'b'
'c') or should i pass in one quote, and use a delimiter I don't plan
to encounter otherwise? Anyone have a quick macro sample that does
this?
Thanks!
From: Ian Whitlock on
On Aug 5, 1:00 pm, chrismarx <chrism...(a)gmail.com> wrote:
> I know this question has been asked before, and I've read many of the
> posts, but I still don't see how to make this work. I wrote the
> following macro to recode data values:
>
> %macro updateTableValue(dataset,varName,orgVals,newVarName,newVals);
>         data &dataset; set &dataset;
>                 %let i = 1;
>                 %do %while (%qscan(&orgVals,&i ) ne );
>                         %let orgVal = %qscan(&orgVals,&i);
>                         %let newVal = %qscan(&newVals,&i);
>                         if &varName = &orgVal then &newVarName = &newVal;
>                         %let i=%eval(&i+1);
>                 %end;
>         run;
>  %mend;
>
>  %updateTableValue(rename_t1, My_family_or_friends_will_enjoy, 'Very
> important' 'Somewhat important', GBBC_TEST2, 1 2)
>
> I'ved tried other variations, but I'm hoping maybe someone can point
> me in the right direction here. Is is advisable to pass in a list of
> values that way i am currently, (ie list of quoted values, 'a' 'b'
> 'c') or should i pass in one quote, and use a delimiter I don't plan
> to encounter otherwise? Anyone have a quick macro sample that does
> this?
> Thanks!

I would suggest:

%macro updateTableValue(dataset,varName,orgVals,newVarName,newVals);
data &dataset; set &dataset;
%let i = 1;
%do %while (%qscan(&orgVals,&i,/) ne );
%let orgVal = %qscan(&orgVals,&i,/);
%let newVal = %qscan(&newVals,&i,/);
if &varName = &orgVal then &newVarName =
&newVal;
%let i=%eval(&i+1);
%end;
run;
%mend;
%updateTableValue(dataset=rename_t1
,varName=My_family_or_friends_will_enjoy
,orgVals='Very important' / 'Somewhat important'
,newVarName=GBBC_TEST2
,newVals=1 / 2)

Ian Whitlock
From: Erica Van Etten on
ah, thank you! that worked great. However, I then saw that that the
split value was still not being evaluated as a string, the only way i
could get the function to work was to write it like this:

%macro updateTableValue(dataset,varName,orgVals,newVarName,newVals);
data &dataset; set &dataset;
%let i = 1;
%do %while (%qscan(&orgVals,&i,/) ne );
%let orgVal = %qscan(&orgVals,&i,/);
if &varName = "%substr(&orgVal,2,%length(&orgVal)-2)" then
&newVarName = %qscan(&newVals,&i,/);
%let i=%eval(&i+1);
%end;
run;
%mend;

specifically this line:

if &varName = "%substr(&orgVal,2,%length(&orgVal)-2)"

if i didnt substring out the original single quotes, and place the
whole value in quotes, i got the error:

ERROR 390-185: Expecting an relational or arithmetic operator.

Is what I'm doing the proper way to try to evaluate a string value?
Thanks again for your help!
chris-



On Aug 5, 4:40 pm, Ian Whitlock <iw1...(a)gmail.com> wrote:
> On Aug 5, 1:00 pm, chrismarx <chrism...(a)gmail.com> wrote:
>
>
>
>
>
> > I know this question has been asked before, and I've read many of the
> > posts, but I still don't see how to make this work. I wrote the
> > following macro to recode data values:
>
> > %macro updateTableValue(dataset,varName,orgVals,newVarName,newVals);
> >         data &dataset; set &dataset;
> >                 %let i = 1;
> >                 %do %while (%qscan(&orgVals,&i ) ne );
> >                         %let orgVal = %qscan(&orgVals,&i);
> >                         %let newVal = %qscan(&newVals,&i);
> >                         if &varName = &orgVal then &newVarName = &newVal;
> >                         %let i=%eval(&i+1);
> >                 %end;
> >         run;
> >  %mend;
>
> >  %updateTableValue(rename_t1, My_family_or_friends_will_enjoy, 'Very
> > important' 'Somewhat important', GBBC_TEST2, 1 2)
>
> > I'ved tried other variations, but I'm hoping maybe someone can point
> > me in the right direction here. Is is advisable to pass in a list of
> > values that way i am currently, (ie list of quoted values, 'a' 'b'
> > 'c') or should i pass in one quote, and use a delimiter I don't plan
> > to encounter otherwise? Anyone have a quick macro sample that does
> > this?
> > Thanks!
>
> I would suggest:
>
> %macro updateTableValue(dataset,varName,orgVals,newVarName,newVals);
>         data &dataset; set &dataset;
>                 %let i = 1;
>                 %do %while (%qscan(&orgVals,&i,/) ne );
>                         %let orgVal = %qscan(&orgVals,&i,/);
>                         %let newVal = %qscan(&newVals,&i,/);
>                         if &varName = &orgVal then &newVarName =
> &newVal;
>                         %let i=%eval(&i+1);
>                 %end;
>         run;
>  %mend;
>  %updateTableValue(dataset=rename_t1
>                   ,varName=My_family_or_friends_will_enjoy
>                   ,orgVals='Very important' / 'Somewhat important'
>                   ,newVarName=GBBC_TEST2
>                   ,newVals=1 / 2)
>
> Ian Whitlock