DATA Step, Macro, Functions and more

Use a macrovariable as argument for a put function

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

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'
	3 = 'Indeterminado'
	other = 'No informado'
	;
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?

 

 

Thanks in advance for all your feedback

 

 


Accepted Solutions
Solution
a week ago
Super User
Posts: 5,516

Re: Use a macrovariable as argument for a put function

Posted in reply to iscgonzalez

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.

View solution in original post


All Replies
Solution
a week ago
Super User
Posts: 5,516

Re: Use a macrovariable as argument for a put function

Posted in reply to iscgonzalez

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.

Occasional Contributor
Posts: 5

Re: Use a macrovariable as argument for a put function

Posted in reply to Astounding
Thanks for your help, this worked flawlessly!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 91 views
  • 1 like
  • 2 in conversation