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)

[ Edited ]

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;

How I am planning to use them is to go through a do loop in data step. So lets say I have a dataset called 'original' 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 original;
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, 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-26-2017 07:42 PM
Super User
Super User
Posts: 7,860

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

[ Edited ]
Posted in reply to chingweelim

The PUT() function requires the format be specified at compile time.

The PUTN() and PUTC() functions will take a character expression that evaluates to the format specification.

Here is a worked example.

proc format library = work ;
value GROUP
 0 = 'Controlled Hypertensive Patients'
 1 = 'Uncontrolled Hypertensive Patients'
;
value COUNTRY
  0 = 'CHINA'
  1 = 'BRITAIN'
  2 = 'USA'
;
run;

%let var_names=GROUP COUNTRY;
%let var_format=GROUP COUNTRY;

data want;
  input &var_names ;
  length Var_Name $32 Var_Value 8 Variable_label $150;
  array values &var_names ;
  do _i=1 to countw(symget('var_format'));
    Var_Name = scan(symget('var_names'),_i);
    Var_Value = values(_i);
    Variable_label = putn( var_value , cats(scan(symget('var_format'),_i),'.') );
    output;
  end;
  drop _i &var_names;
cards;
0 0
0 1
1 2
;
                    Var_
Obs    Var_Name    Value    Variable_label

 1     GROUP         0      Controlled Hypertensive Patients
 2     COUNTRY       0      CHINA
 3     GROUP         0      Controlled Hypertensive Patients
 4     COUNTRY       1      BRITAIN
 5     GROUP         1      Uncontrolled Hypertensive Patients
 6     COUNTRY       2      USA

P.S. Don't use FORMAT when you mean LENGTH.  FORMAT statement is to attache formats to variables so SAS will know how to print them. SAS already knows how to print character variables, so there is no need to attach the $150. format to the variable VARIABLE_LABEL. 

View solution in original post


All Replies
Contributor RM6
Contributor
Posts: 24

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

Posted in reply to chingweelim
can you post some more details, what i understood is that you are trying to apply 2 formats to the same variable.
Solution
‎11-26-2017 07:42 PM
Super User
Super User
Posts: 7,860

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

[ Edited ]
Posted in reply to chingweelim

The PUT() function requires the format be specified at compile time.

The PUTN() and PUTC() functions will take a character expression that evaluates to the format specification.

Here is a worked example.

proc format library = work ;
value GROUP
 0 = 'Controlled Hypertensive Patients'
 1 = 'Uncontrolled Hypertensive Patients'
;
value COUNTRY
  0 = 'CHINA'
  1 = 'BRITAIN'
  2 = 'USA'
;
run;

%let var_names=GROUP COUNTRY;
%let var_format=GROUP COUNTRY;

data want;
  input &var_names ;
  length Var_Name $32 Var_Value 8 Variable_label $150;
  array values &var_names ;
  do _i=1 to countw(symget('var_format'));
    Var_Name = scan(symget('var_names'),_i);
    Var_Value = values(_i);
    Variable_label = putn( var_value , cats(scan(symget('var_format'),_i),'.') );
    output;
  end;
  drop _i &var_names;
cards;
0 0
0 1
1 2
;
                    Var_
Obs    Var_Name    Value    Variable_label

 1     GROUP         0      Controlled Hypertensive Patients
 2     COUNTRY       0      CHINA
 3     GROUP         0      Controlled Hypertensive Patients
 4     COUNTRY       1      BRITAIN
 5     GROUP         1      Uncontrolled Hypertensive Patients
 6     COUNTRY       2      USA

P.S. Don't use FORMAT when you mean LENGTH.  FORMAT statement is to attache formats to variables so SAS will know how to print them. SAS already knows how to print character variables, so there is no need to attach the $150. format to the variable VARIABLE_LABEL. 

Occasional Contributor
Posts: 11

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

Thank you Tom for the advice! I did not know you could use putn() or putc() to take in character expressions in the second argument. Worked like a charm.

☑ This topic is solved.

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

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