Solved
Contributor
Posts: 24

# Use a macrovariable as argument for a put function

Hi Guys,

I'm struggling with this and I hope someone can give some light on the issue here

I have a sample dataset with the column sex (coded in numbers from 1 to 3)  and the column lang (language). Also I have 2 formats: sexEn and sexEs that decode the values on the sex column depending on the language.

What I need is to create a new variable that has the sex decoded on the proper language. See code below:

```proc format;
value sexEn
1 = 'Female'
2 = 'Male'
3 = 'Indeterminate'
other = 'Unknown'
;
value sexEs
1 = 'Femenino'
2 = 'Masculino'
;
run;

/*for generating the sample dataset*/
%MACRO RandBetween(min/*valor minimo*/, max /*=valor máximo*/);
(&min. + floor((0+&max.-&min.)*rand("uniform")))
%MEND;

/* steps to generate sample datasete from here */
data test;
do i=1 to 10;
lan = %RandBetween(1,3);
sex = %RandBetween(1,4);
output;
end;
run;

data test2;
set test;
if lan = 1 then
lang= 'Es';
else lang = 'En';
drop lan;
run;
/*******************to here****************/

/*here I create macrovariables with the language for each row*/
data _null_;
do i = 1 by 1 until (eof);
set test2 end=eof;
call symputx(cats('formsex',i),lang);
end;
run;

/*I need to create a new variable here called sexformatted
by using the needed format
*/
data test3;
do i = 1 by 1 until (eof);
set test2 end=eof;
sexformatted = put(sex,cats('sex',symget(cats('formsex',i)))..);
output;
end;
run;
```

but I get an error saying that it is expecting a format name on

``sexformatted = put(sex,cats('sex',symget(cats('formsex',i)))..);``

Is there a way to get around this error? May I be doing something wrong?

Accepted Solutions
Solution
‎11-14-2017 01:44 PM
Super User
Posts: 6,171

## Re: Use a macrovariable as argument for a put function

Consider using PUTN instead of PUT.  PUTN allows the second parameter (the name of the format) to be an expression rather than a hard-coded name.  So if you keep the variable LANG, you should be able to use:

sexformatted = putn(sex, cats('sex', lang) ) ;

That also lets you skip some of the complications of creating macro variables.

All Replies
Solution
‎11-14-2017 01:44 PM
Super User
Posts: 6,171

## Re: Use a macrovariable as argument for a put function

Consider using PUTN instead of PUT.  PUTN allows the second parameter (the name of the format) to be an expression rather than a hard-coded name.  So if you keep the variable LANG, you should be able to use:

sexformatted = putn(sex, cats('sex', lang) ) ;

That also lets you skip some of the complications of creating macro variables.

Highlighted
Contributor
Posts: 24