DATA Step, Macro, Functions and more

Placing macro variable(functions) into second argument of put(source,format)

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Placing macro variable(functions) into second argument of put(source,format)

I have a few proc format values which I want to use in the second argument of put(source,format). For example:

proc format library = work ;
    value GROUP
	0 = 'Controlled Hypertensive Patients'
	1 = 'Uncontrolled Hypertensive Patients';


    value COUNTRY
        0 = 'CHINA'
        1 = 'BRITAIN'
        2 = 'USA';
run;

I plan to use them in a do loop in data step. So lets say I have a dataset called 'have' that contains variables 'GROUP' and 'COUNTRY' (same names as the value formats). This is what I tried:

 

%let var_format=GROUP COUNTRY

data want;
set have;
format Variable_label $150.;
do _i=1 to countw(symget('var_format'));
    Variable_label = put( scan(symget('var_format'),_i) , scan(symget('var_format'),_i). ); end; drop _:; run;

Unfortunately, in the second argument of the put(source,format) statement, it gave the following error in the log:

 

Variable_label = put(scan(symget('var_format'),_i),scan(symget('var_format'),_i).);
                                                   ----
                                                    85
                                                    76
ERROR 85-322: Expecting a format name.

ERROR 76-322: Syntax error, statement will be ignored.

As such, is there anyway around this problem?


Accepted Solutions
Solution
‎11-23-2017 10:51 PM
Trusted Advisor
Posts: 1,828

Re: Placing macro variable(functions) into second argument of put(source,format)

Posted in reply to chingweelim

PUT function requires a format as second argument. (See error message)

Format should end with a dot and optionally length, so you need define:

%let var_format=GROUP COUNTRY. ;

 

You can use function PUTC (PUTN for numeric ) with next syntax:

   

PUTC(source, format.<,w>) 

and I would assign a temporary variable:

      _varx = symget('var_format');

and replace all symget('var_format') with the _varx.

 

 

View solution in original post


All Replies
Solution
‎11-23-2017 10:51 PM
Trusted Advisor
Posts: 1,828

Re: Placing macro variable(functions) into second argument of put(source,format)

Posted in reply to chingweelim

PUT function requires a format as second argument. (See error message)

Format should end with a dot and optionally length, so you need define:

%let var_format=GROUP COUNTRY. ;

 

You can use function PUTC (PUTN for numeric ) with next syntax:

   

PUTC(source, format.<,w>) 

and I would assign a temporary variable:

      _varx = symget('var_format');

and replace all symget('var_format') with the _varx.

 

 

Super User
Posts: 22,875

Re: Placing macro variable(functions) into second argument of put(source,format)

Posted in reply to chingweelim

PUTC and PUTN will take a text argument for the second parameter so you don’t need this macro implementation. 

At lear

Occasional Contributor
Posts: 11

Re: Placing macro variable(functions) into second argument of put(source,format)

Hi Reeza and Shmuel,

 

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 102 views
  • 0 likes
  • 3 in conversation