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

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?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
RM6
Obsidian | Level 7 RM6
Obsidian | Level 7
can you post some more details, what i understood is that you are trying to apply 2 formats to the same variable.
Tom
Super User Tom
Super User

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. 

chingweelim
Calcite | Level 5

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3 replies
  • 1307 views
  • 0 likes
  • 3 in conversation