0

I wanted to come here with this challenge I'm facing in these days.

Basically for each record, a different format should be used in a put statement, and it is defined in the data in theirselves.

The challenge is not to split the datasteps and gaining the wanted result within the datastep, so avoid obvious %do loops and similar :)

proc format;
    value $a 'FRS'='FIRST';
    value $b 'SCN'='SECOND';
run;
data a;
    length var $3 res $10 fmt $5;
    var='FRS'; fmt='$a.'; res=''; output;
    var='SCN'; fmt='$b.'; res=''; output;
run;
data b;
    set a;

    *your code goes here, the result should go into res variable;
    *and should be the "putted" value of var using fmt as format.;
    *an obviously non working version can be found here below;

    res=put(var,fmt);
run;

Here below what it looks like, res is the expected result:

VAR  FMT  RES
---------------
"FRS" $a. =put("FRS",$a.)="FIRST"
"SCN" $b. =put("SCN",$b.)="SECOND"
stat
  • 699
  • 3
  • 10

1 Answers1

6

I am not sure I understand the question, but it looks like you just want to use the PUTC() function. If your variable was numeric you would use the PUTN() function instead.

res=putc(var,fmt);
Tom
  • 47,574
  • 2
  • 16
  • 29
  • unfortunately you can't use a variable as format :( – stat May 29 '18 at 14:07
  • in the PUT function that is correct but @tom has shown you PUTC. – data _null_ May 29 '18 at 14:09
  • 5
    @stat You can definitely use a variable to contain the format specification with `PUTC` and `PUTN` function. That is one of the main differences between them and the `PUT` function. – Tom May 29 '18 at 14:09