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

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 ?

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

4 REPLIES 4
PGStats
Opal | Level 21

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
mathias
Quartz | Level 8

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

Tom
Super User Tom
Super User

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()
mathias
Quartz | Level 8

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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