Help using Base SAS procedures

Manipulating the output of SAS with quotation marks

Reply
Contributor
Posts: 32

Manipulating the output of SAS with quotation marks

quHey community,

I am creating an output with SAS which becomes an input for another program. The main requirement is that strings should look like this: "STRING".

This is what I tried:

%LET MP_FILENAME = DCLR1L;

%LET MP_format = Rente;

data mp_&MP_format.;

length Pol_number $ 11 init_prem 8 filename $ 6;

pol_number = "01234567890";

pol_number2 = cat('"', pol_number,'"');

init_prem = 1000;

filename = "DCLR1L";

run;

%let mp_file = /appdata/sas/fb_daten/vrm/lp/&MP_FILENAME..pro;

%let termstr = termstr %str(=) crlf;

data _null_;

F = "&mp_file.";

file Z filevar=F mod DLM= ',' &termstr. lrecl=10000 DSD;

put "Output_Format , &MP_format.";

put "T1,T11,N,T6";

put "!,Pol_number,init_prem,filename";

run;

In SAS the result looks perfectly fine: my string has the quotation marks. If I open my output file in Windows, the string has not only one but three quotation marks on each side...?


SAS is running on another server under Unix OS and I need to have the output on another Server with Windows OS. Could it be that the conversion from Unix to Windows causes these problemes?

I am thankful for any help/idea.

Super User
Super User
Posts: 7,401

Re: Manipulating the output of SAS with quotation marks

Hi,

There are various posts on the topic, this one from yesterday may help as a start:

https://communities.sas.com/thread/63863

Contributor
Posts: 32

Re: Manipulating the output of SAS with quotation marks

Hi RW9,

thanks a lot for the quick and helpful answer.

I was able to apply the workflow to my data. Unfortunately I only need the quotes for character variables but I don't know where/how to restrict the set of variables in the workflow.

Could you help me out once more?

Super User
Super User
Posts: 7,401

Re: Manipulating the output of SAS with quotation marks

No probs.  If you are using my code you should be able to modify slightly to incorporate another if statement for char or num:

filename temp 's:\temp\rob\test.txt' ;

data temp;
  set sashelp.vcolumn (where=(libname="SASHELP" and memname="CLASS")) end=last;
  length lst_list $2000.;
  retain lst_list;
  if _n_=1 then do;
if type="char" then lst_list='"'||strip(name)||'";';
else lst_list=strip(name)||';';
  end;
  else do;
if type="char" then lst_list=strip(lst_list)||'"'||strip(name)||'";';
else lst_list=strip(lst_list)||strip(name)||';';
  end;
  if last then do;
    call execute('data _null_; file temp dlm=";" dsd lrecl=3000; set sashelp.class;');
    call execute(" put '"||strip(lst_list)||"';");
    call execute('; put (_all_)(~); run;');
  end;
run;

Contributor
Posts: 32

Re: Manipulating the output of SAS with quotation marks

Hi RW9,

it's me once again. Hopefully you are still not annoyed by my questions.

In the output file, SAS reserved the 2000 Bytes space for the character variables as defined in the code. This results in spaces in the file.

The program which now reads the file, has a problem with the additional spaces. My idea would be to alter the length of the variable in the code but whatever I try, SAS is complaining.... All available functions in SAS like trim, etc. do not work since the variable itself does not contain any spaces.

How can I alter the length of the variable?

I would like to use something like

length var_list2 $ length(var_list);

var_list2=var_list;

where var_list is my variable from the code with length 2000.

But this results in an error....

Do you have an idea?

Super User
Super User
Posts: 7,401

Re: Manipulating the output of SAS with quotation marks

Could you put your code in, and output example as I am not following you.

Contributor
Posts: 32

Re: Manipulating the output of SAS with quotation marks

Sure:

My complete code is as follows:

%let MP_FILENAME = DCR1L;

%let MP_format= Rente;

%let star = *,;

%let mp_file = /appdata/sas/fb_daten/vrm/lp&MP_FILENAME..pro;

%let termstr = termstr %str(=) crlf;

%macro MP_header ;

data _null_;

     F = "&mp_file.";

     file Z filevar=F mod DLM= ',' &termstr. lrecl=10000 DSD;

     put "Output_Format , &MP_format.";

     put "&typ_list.";

     put "&var_list.";

run;

%mend;

%macro MP_fill ;

data CSV_Rente;

set MP_Tabelle;

F = "&mp_file.";

file Z filevar=F mod DLM= ',' &termstr. lrecl=10000 DSD;

if _N_ < 11 then SPCODE = _N_;

Else SPCODE = 99;

put  "&star" &put_list.;

run;

%mend;

proc fcmp outlib=sasuser.funcs.sql;

function fct_MP_header();

rc = run_macro('MP_header');

return (rc);

endsub;

proc fcmp outlib=sasuser.funcs.sql;

function fct_MP_fill();

rc = run_macro('MP_fill');

return (rc);

endsub;

data _null_;

set Var_Names end=eof nobs=nos;

F = "&mp_file.";

length typ_list $2000.;

length var_list $2000.;

length put_list $2000.;

retain typ_list;

retain var_list;

retain put_list;

if _n_=1 then do;

     typ_list='VARIABLE_TYPES,T1,I,N,';

     var_list='!,SPCODE,AGE_AT_ENTRY,';

      put_list='(SPCODE)(+0)(AGE_AT_ENTRY)(+0)';

End;

else if _n_=nos then do;

     var_list=strip(var_list)||strip(name);

          if type=2 then do;

               put_list=strip(put_list)||'('||strip(name)||')(~)';

               typ_list=strip(typ_list)||'T'||strip(length);

          end;

          else do;

               put_list=strip(put_list)||'('||strip(name)||')(+0)';

                typ_list=strip(typ_list)||'N';

          end;

end;

Else do;

     var_list=strip(var_list)||strip(name)||',';

     if type=2 then do;

         put_list=strip(put_list)||'('||strip(name)||')(~)';

          typ_list=strip(typ_list)||'T'||strip(length)||',';

     end;

     else do;

          put_list=strip(put_list)||'('||strip(name)||')(+0)';

          typ_list=strip(typ_list)||'N,';

     end;

end;

if eof then do;

     call symput('put_list', put_list);

     call symput('var_list', var_list);

     call symput('typ_list', typ_list);

     rc=fct_MP_header();

     rc=fct_MP_fill();

     end;

run;

I attached the output file DCLR2L.pro with the output. If you open the file with an editor, you will notice that SAS procuded blanks for the three lists to fill up the 2000 bytes.

Attachment
Super User
Super User
Posts: 7,401

Re: Manipulating the output of SAS with quotation marks

I would imagine its these:

     put "&typ_list.";

     put "&var_list.";

Change them to:

     put "%trim(&typ_list.)";

     put "%trim(&var_list.)";

Contributor
Posts: 32

Re: Manipulating the output of SAS with quotation marks

That's for the quick answer.

If I change as you suggested, I get the error:

ERROR: More positional parameters found than defined.

But if I change the quotation marks as follows:

     put %trim("&typ_list.");

     put %trim("&var_list.");

I don't get an error but the result is still the same.

If I understand correctly, the %trim removes all blanks of the variables, but as I try to explain, the variables itself don't have any blanks.

The problem seems to be the allocated space of 2000 bytes for each string. In the output, the remaining bytes are filled up with blanks.

If I manually change, for example the length of var_list to 1011 (length(var_list)=1011 in this case) instead of 2000, there are no blanks. But as the length varies, this solution is not satisfactory...

Contributor
Posts: 32

Re: Manipulating the output of SAS with quotation marks

Me again:

Somehow directly it didn't work but like this:

var_list2=%trim("&var_list.");

put var_list2;

the output finally leaves out the blanks BUT I have quotation marks in the beginng and at the end... Any idea how I can get rid of them?

Super User
Posts: 9,681

Re: Manipulating the output of SAS with quotation marks

var_list2="%trim(&var_list)";

put var_list2;

Contributor
Posts: 32

Re: Manipulating the output of SAS with quotation marks

Hi Xia Keshan,

thanks for joining!

Like this, I get the error message:

ERROR: More positional parameters found than defined.

Super User
Posts: 9,681

Re: Manipulating the output of SAS with quotation marks

Sorry . I didn't go through all of this post . don't know its context  , maybe RW9 could answer your question.

Contributor
Posts: 32

Re: Manipulating the output of SAS with quotation marks

No need for an excuse. Any help is welcome. Smiley Happy

If I put the quotes like this:

var_list2=%trim("&var_list.");

and then do put var_list2, I see in the log, that the variable does not contain any leading or trailing quotes.

But if the variable is written to the file, the quotes appear...

Contributor
Posts: 32

Re: Manipulating the output of SAS with quotation marks

I found the problem:

I should leave out the DSD option in the MP_header macro...

Sorry for the inconvenience, guys.

Thanks again for all your help!

Ask a Question
Discussion stats
  • 16 replies
  • 471 views
  • 9 likes
  • 4 in conversation