SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
acordes
Rhodochrosite | Level 12

It's more for the readability than for the functionality. 

 

proc contents data=sashelp.cars out=namy;
run;

%let abc=put text here;

ods escapechar = '`';
proc sql noprint;
select catx(' ', '{name=', quote(strip(name)), "label=", %sysfunc(quote("&abc")), '}') into :oks separated by ", (*ESC*) {unicode '000A'x}  "
from WORK.namy where label = '';
quit;

%put %bquote(&oks.);

The log shows:

 

{name= "Cylinders" label= "put text here" }, (*ESC*) {unicode '000A'x}  {name= "DriveTrain" label= "put text here" }, (*ESC*) 
{unicode '000A'x}  {name= "Horsepower" label= "put text here" }, (*ESC*) {unicode '000A'x}  {name= "Invoice" label= "put text here" 
}, (*ESC*) {unicode '000A'x}  {name= "MSRP" label= "put text here" }, (*ESC*) {unicode '000A'x}  {name= "Make" label= "put text 
here" }, (*ESC*) {unicode '000A'x}  {name= "Model" label= "put text here" }, (*ESC*) {unicode '000A'x}  {name= "Origin" label= "put 
text here" }, (*ESC*) {unicode '000A'x}  {name= "Type" label= "put text here" }

 But I would like to have:

%put %bquote(&oks.);
{name= "Cylinders" label= "put text here" }, 
{name= "DriveTrain" label= "put text here" }, 
{name= "Horsepower" label= "put text 
here" }, 
{name= "Invoice" label= "put text here" }, 
{name= "MSRP" label= "put text here" }, 
{name= "Make" label= "put text here" }, 
{name= "Model" label= "put text here" }, 
{name= "Origin" label= "put text here" }, 
{name= "Type" label= "put text here" }
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Why does the "want" for Horsepower show a line break after "text"? That's going to be a serious difference of logic if intentional.

 

Where do you expect this to display? Is this only for the log or elsewhere? I'm not sure that the unicode characters display well in the LOG.

Any particular reason you didn't use the NEWLINE function instead of the unicode character? Not that the LOG likes that any better in a macro variable.

 

Here's an example, that depending on your settings may display something similar to what you want in the results window using FILE PRINT with PUT. Remove the File Print and see what the LOG shows for why I commented about unicode and inline formatting.

Then a Proc Print of a variable built.

data junk (keep=lngstr);
   set namy (where= (name in ("Cylinders" "DriveTrain"))) end=lastone ;
   length lngstr $5000 ;
   retain lngstr;
   lngstr = catx(",(*ESC*){newline}",strip(lngstr),
                 catx(' ', '{name=',quote(strip(name)), "label=", quote(strip("&abc")), '}'));
   file print;
   if lastone then do;
      put lngstr;
      call symputx('oks2',strip(lngstr));
      output;
   end;
run;
%put %bquote(&oks2.);

proc print data=junk noobs;
  var lngstr;
run;

View solution in original post

4 REPLIES 4
ballardw
Super User

Why does the "want" for Horsepower show a line break after "text"? That's going to be a serious difference of logic if intentional.

 

Where do you expect this to display? Is this only for the log or elsewhere? I'm not sure that the unicode characters display well in the LOG.

Any particular reason you didn't use the NEWLINE function instead of the unicode character? Not that the LOG likes that any better in a macro variable.

 

Here's an example, that depending on your settings may display something similar to what you want in the results window using FILE PRINT with PUT. Remove the File Print and see what the LOG shows for why I commented about unicode and inline formatting.

Then a Proc Print of a variable built.

data junk (keep=lngstr);
   set namy (where= (name in ("Cylinders" "DriveTrain"))) end=lastone ;
   length lngstr $5000 ;
   retain lngstr;
   lngstr = catx(",(*ESC*){newline}",strip(lngstr),
                 catx(' ', '{name=',quote(strip(name)), "label=", quote(strip("&abc")), '}'));
   file print;
   if lastone then do;
      put lngstr;
      call symputx('oks2',strip(lngstr));
      output;
   end;
run;
%put %bquote(&oks2.);

proc print data=junk noobs;
  var lngstr;
run;
acordes
Rhodochrosite | Level 12

Hi @ballardw , that's it.

I use it to create a code snippet that I paste to a the cas action altertable.

And for the sake of readability of the code I was looking the linebreak. 

 

cas mySession sessopts=(caslib="public");
proc cas;                                                                   
   table.alterTable / 
	columns={                                          
		{name= "BATCH_ID" label= "BATCH ID derived from recipe and its start time" }, 
		{name= "BATCH_ID_dt" label= "BATCH ID derived from recipe and its start time in datetime format" }
		},
   name="have_table";
   table.fetch / table="have_table"                                        
   fetchvars={"&vars"},
index=false;
quit;

 

quickbluefish
Lapis Lazuli | Level 10

Here's another option if you want to save them into separate macro variables:


proc contents noprint data=sashelp.cars out=namy (keep=name label); run;

data _null_;
set namy end=last;
nm=quote(strip(name));
lbl=quote("put text here");
c=',';
if last then c='';
msg=compbl('{ name= ' || nm ||' label=' || lbl || '}' || c);
put msg;
call symputx(compress("msg" || _N_), msg);
run;

* for example, the 3rd name/label combination ;
%put MSG3: &msg3;
Tom
Super User Tom
Super User

Create CODE SNIPPETS??

Can you clarify what that means.

If you want to insert linebreak that it put the data into a FILE, not a macro variable.

filename snippet temp;
proc contents data=sashelp.class noprint out=contents; run;
proc sort data=contents;
  by varnum;
run;

data _null_;
  file snippet;
  set contents end=eof;
  if _n_=1 then put @2 'columns=' / @4 '{' @;
  else put @4 ',' @;
  put '{' name= :$quote. label= :$quote. '}';
  if eof then put @4 '},' ; 
run;

Result:

 columns=
   {{NAME="Name" LABEL="" }
   ,{NAME="Sex" LABEL="" }
   ,{NAME="Age" LABEL="" }
   ,{NAME="Height" LABEL="" }
   ,{NAME="Weight" LABEL="" }
   },

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Save $200 when you sign up by March 14!

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
  • 4 replies
  • 376 views
  • 3 likes
  • 4 in conversation