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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 2 replies
  • 718 views
  • 1 like
  • 2 in conversation