DATA Step, Macro, Functions and more

apply format to comma separated values

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 124
Accepted Solution

apply format to comma separated values

Hi,

I have a string (that comes from a multiple select in a stp)

that looks like this:

('BORPER','BRR_SP','CHL_PN')

and I have a format for these 3 values (e.g. BORPER="Bordetella pertussis")

Is there a way to apply my format to each value of my string rather than the whole string ?


Accepted Solutions
Solution
‎03-07-2013 12:28 PM
Respected Advisor
Posts: 4,646

Re: apply format to comma separated values

Something like this?

data test;
attrib line length=$100;
input line &;
datalines;
('BORPER','BRR_SP','CHL_PN')
;

proc Format;
value $ myFormat
     "BORPER"="Bordetella pertussis"
     "BRR_SP"="Some sort of BRR"
     "CHL_PN"="What is CHL PN?"

     OTHER="Other species"
;
run;

data want(keep=line newline);
attrib newLine length=$100;
set test;
do i = 1 by 1;
word = scan(line,i,"'(), ");
if missing(word) then leave;
newLine = catx(",",newLine,cats("'",put(word,$myFormat.),"'"));
end;
newLine = cats('(',newLine,')');
run;

PG

PG

View solution in original post


All Replies
Solution
‎03-07-2013 12:28 PM
Respected Advisor
Posts: 4,646

Re: apply format to comma separated values

Something like this?

data test;
attrib line length=$100;
input line &;
datalines;
('BORPER','BRR_SP','CHL_PN')
;

proc Format;
value $ myFormat
     "BORPER"="Bordetella pertussis"
     "BRR_SP"="Some sort of BRR"
     "CHL_PN"="What is CHL PN?"

     OTHER="Other species"
;
run;

data want(keep=line newline);
attrib newLine length=$100;
set test;
do i = 1 by 1;
word = scan(line,i,"'(), ");
if missing(word) then leave;
newLine = catx(",",newLine,cats("'",put(word,$myFormat.),"'"));
end;
newLine = cats('(',newLine,')');
run;

PG

PG
Frequent Contributor
Posts: 124

Re: apply format to comma separated values

Works great outside a macro, but how would you run this inside a macro (which does not accept datalines statements) ?

Super User
Super User
Posts: 6,500

Re: apply format to comma separated values

Huh? 

The data step was just there to setup an example dataset so that PG could post a working example.

What is the source of the string?

  • If it is already in a dataset then process the dataset as PG suggests.
  • If it is in a macro variable then
    • you could just replace his SET TEST line with LINE="&macrovarname" statement instead.
    • Or you could translate the data step into the equivalent statements in macro code use %SYSFUNC()
Frequent Contributor
Posts: 124

Re: apply format to comma separated values

Big thanks to PGstats !

found it with proc sql :

proc sql;

  create table test

  (germs char(100));

  quit;

  proc sql;

  insert into test

  (germs)

  values("('BORPER','BRR_SP','CHL_PN')");

  quit;

  data _null_(keep=germs latinGerm);

  attrib latinGerm length=$100;

  set test;

  do i = 1 by 1;

  word = scan(germs,i,"'() ,'");

  if missing(word) then leave;

  latinGerm = catx(" & ",latinGerm,cats("",put(word,&formatGerm.),""));

  end;

  /*latinGerm = cats('(',latinGerm,')');*/

  call symputx ('displayGerms',latinGerm);

  run;

  %put &displayGerms;

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 304 views
  • 3 likes
  • 3 in conversation