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?
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.
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.
PUTC and PUTN will take a text argument for the second parameter so you don’t need this macro implementation.
At lear
Hi Reeza and Shmuel,
Thank you so much for your help. I did not realize there is a function that allows for text as the second argument. Works like a charm!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.