Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning a format to a macro variable

Status
Not open for further replies.

kaeserea

Programmer
Feb 26, 2003
164
0
0
DE
Hello!

I have a macro variable to which I'd like to assign a format:

[tt]
proc format;
value $fmedium 'P' = 'Paper'
'F' = 'Fax'
'E' = 'Email';
run;
[/tt]

The macro variable has for example the value P and should be displayed as Paper.

Can anybody help?

Best regards
Eva
 
Can you show how you're using the Macro Variable to display?
Easiest way I can think of is to use it to create a new variable, then display that...
Code:
  newvar = put(&MVAR,$fmedium.);
  put newvar=;


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Hello Chris,

I do it just like that in a data _null_ step to display the variable in a HTML Heading:

[tt]
DATA _NULL_;
FILE _WEBOUT &g_sWEBOUT_OPTIONS.;
if &medium ne all then medium_long = put(&medium,$fmedium.);
else medium_long = "all";

put "<h2>My Heading</h2>";
put "Medium: " medium_bez;

etc......

run;
[/tt]

But I get the error
[tt]
190 + if &medium ne all then medium_long = put(&medium,$fmedium.);
_________
484
WARNING: Variable P has already been defined as numeric.
[/tt]

Beforhand I get &medium from another stored process via the url and this stored process receives it as a parameter.

The error also occurs when I start the STP single and type the parameter in at the standard page in the SAS portal.

Bets regards
Eva
 
Ah!
Sorry, I missed some quotes there...
Code:
newvar = put("&MVAR",$fmedium.);
put newvar=;

The original code I did would work is the values were numeric, however as it was a character that came through, SAS interpreted it as being a variable name, which did not exist and was therefore immediately created as a numeric field.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Hello Chris,

thanx a lot, it works. However I had to add some more quotes at the if in the data step:

[tt]if "&medium" ne all then medium_long = put("&medium",$fmedium.);
[/tt]

So the lesson ist, alsways use quotes when a macro variable should be recognized as character...

Yet I'm a little disturbed at that. I thought that all macro variables are automatically character. Or am I wrong?

Best regards
Eva
 
No, you are right.
However, what you need to do to understand why it didn't work is to see exactly how SAS treats the macro variable.
The macro variable name gets replaced by it's value at run time, so the line of code that was run by SAS is:-
Code:
  if P ne all then medium_long = put(P,$medium);
which obviously doesn't work. By putting the quotes in you get
Code:
  if "P" ne all then medium_long = put("P",$medium);
Remember to use " marks rather than ', as single quotes denote a literal string, so it will literally take &MEDIUM as the value to check, rather than the value of &MEDIUM.
I hope that helps.
Chris.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Hi Chris,

thanx for the explanation. Now I understand.

Best regards
Eva
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top