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

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

2 REPLIES 2
Astounding
PROC Star

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.

iscgonzalez
Obsidian | Level 7
Thanks for your help, this worked flawlessly!

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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