BookmarkSubscribeRSS Feed
w020637
Calcite | Level 5
I need to create a tab dlm file out of a sas dataset with label names.

So if I can achieve either of the following it would work.

a. I am able to get the labels using proc print but tab delimited is a problem.
b. I am able to create tab delimited using proc export but getting the labels is a problem.

Would request sas gurus to help me out.

Regards
24 REPLIES 24
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
SAS-supplied macro %DS2CSV can generate such an output.

Scott Barry
SBBWorks, Inc.
Cynthia_sas
SAS Super FREQ
Hi:
Also,
[pre]
ODS CSV FILE='xxxxxx.csv';
proc print data=sashelp.class label;
run;
ODS CSV CLOSE:
[/pre]

will do what you want.

cynthia
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
I read the OP as wanting tab-delimited output, rather than comma-delimited. I looked briefly for some ODS CSV parameter option but found none.

Scott Barry
SBBWorks, Inc.
w020637
Calcite | Level 5
Yes, Scott, The objective is to get a tab delimited file.

Thanks

w020637
Cynthia_sas
SAS Super FREQ
Sorry for the incomplete answer. There is in 9.2, a delimiter= option that allows you to change the delimiter from a comma to a tab character (which is 09'x, I believe) for ODS CSV.

If you invoke this way, you will see what the options are:
[pre]
ods csv file='c:\temp\csvhelp.csv'
options(doc='Help');
[/pre]

Then the delimiter option goes in parens:
[pre]
ods csv file='c:\temp\tabhelp.txt'
options(doc='Help' delimiter="09"x);
proc print data=sashelp.class(obs=3) label;
run;
ods csv close;
[/pre]

cynthia
data_null__
Jade | Level 19

> There is in 9.2, a
> delimiter= option that allows you to change the
> delimiter from a comma to a tab character (which is
> 09'x, I believe) for ODS CSV.

That will be a nice feature. I look forward to using 9.2, soon perhaps.

The features of the FILE and PUT statement plus the call routine VNEXT make the data step solution fairly concise.

filename FT24F001 temp lrecl=512;
data _null_;
   file FT24F001 dsd dlm='09'x;
   set sashelp.shoes(obs=3);
   if _n_ eq 1 then link nameRow;
   put (_all_)(:);
   return;
 nameRow:
   length _LABEL_ $128 _NAME_ $32;
   if _n_ eq 1 then do;
      do while(1);
         call vnext(_name_);
         if _name_ eq '_LABEL_' then leave;
         _label_ = vlabelx(_name_);
         put _label_ @;
         end;
      put;
      end;
   return;
   run;


data _null_;
   infile FT24F001;
   input;
   list;
   run;

Capture.PNG

w020637
Calcite | Level 5
Hi guys,

I am using 9.1 and so the above solutions are not working for me. Appreciate the alacrity with which you guys have responded.

Regards

w020637
Cynthia_sas
SAS Super FREQ
Hi:
I believe that you can download a copy of the CSV tagset template that will work for SAS 9.1.3 from this site:
http://support.sas.com/rnd/base/ods/odsmarkup/ (scroll down until you see the link for the CSV tagsets).

The SUGI paper that describes the OPTIONS suboption list and the DELIMITER suboption was written for SAS 9.1.3, so I believe you CAN get the delimiter, you just have to do some extra work because it's not included automatically with SAS (as it is in 9.2).
http://support.sas.com/rnd/base/ods/odsmarkup/p236-31.pdf

Although this note talks about installing and using the ExcelXP tagset template, the information about installing a tagset template and using the ODS PATH statement, also apply for updates to the CSV tagset template:
http://support.sas.com/kb/32/394.html

cynthia
data_null__
Jade | Level 19
But my program does work in V9.1. You just assumed it would not work?
ChrisNZ
Tourmaline | Level 20
Sorry for reviving this thread, I got pointed to it from another thread.
data _null_, I like the simplicity of your data step, and the way you got around enumerating variables.
Fyi, I made a small macro out of it, for those (frequent) cases when tweaking the output is not needed.
[pre]

%macro write_CSV(fileref= /* REQD: export fileref */
,dataset= /* REQD: dataset name, including options if necessary */
,delimiter=',' /* OPTN: eg: '09'x and ' ' for tab and space */
,firstrow=LABEL /* OPTN: Values: LABEL or NAME or NONE */
,addquotes=SPECIALCHAR /* OPTN: Values: ALLVAR or ALLCHAR or SPECIALCHAR */
);
data _null_;
set &dataset;
file &fileref dlm=&delimiter %if %upcase(&addquotes) ne ALLCHAR %then dsd;;
%if %upcase(&addquotes)=ALLCHAR %then format _CHARACTER_ $quote.;;
%if %upcase(&firstrow) ne NONE %then if _N_ = 1 then link firstrow;;
put (_ALL_)(%if %upcase(&addquotes)=ALLVAR %then~;:);
length __LABEL__ $256 __NAME__ $32;
return;
firstrow:
do while( __NAME__ ne '__LABEL__' );
call vnext(__NAME__);
__LABEL__ = vlabelx(__NAME__);
if __NAME__ ne '__LABEL__' then put __%upcase(&firstrow)__ @;
else put;
end;
run;
%mend;
[/pre]
One question though: how come all PDV variables are not output when put (_ALL_) is run?
Andre
Obsidian | Level 7
Chris
Your macro must be completed
as in the case of an ALLCHAR choice there is an error

[pre]
file &fileref dlm=&delimiter %if %upcase(&addquotes) ne ALLCHAR %then dsd%str(;);
%else %str(;);
[/pre]

Andre from Paris ( not linked to any submarine activity 😉 )
ChrisNZ
Tourmaline | Level 20
Fixed, André, thank you.
I usually try to end my %if tests with a double ;; to remove %str() clutter, but I thought I'd post a more formal version... I have reverted to my old self now. That will teach me! 🙂

Do you know why some PDV variables are not output when put (_ALL_) is run? I am still perplexed.
Cynthia_sas
SAS Super FREQ
Chris:
I ran a much simpler test and found that the simple PUT _ALL_ writes out everything, including _N_ and _ERROR_, as shown below. If you're seeing something else, you might want to work with Tech Support or redo your macro to see if you can figure out where things might be going wrong.

cynthia
[pre]
data _null_;
set sashelp.shoes(obs=5);
file 'c:\temp\test_putall.txt';
format _CHARACTER_ $quote.;
format _NUMERIC_;
if _N_ = 1 then link firstrow;
put _ALL_;
return;
firstrow:
put 'What will be written out?';
return;
run;

Output:
What will be written out?
Region="Africa" Product="Boot" Subsidiary="Addis Ababa" Stores=12 Sales=29761 Inventory=191821 Returns=769 _ERROR_=0 _N_=1
Region="Africa" Product="Men's Casual" Subsidiary="Addis Ababa" Stores=4 Sales=67242 Inventory=118036 Returns=2284 _ERROR_=0 _N_=2
Region="Africa" Product="Men's Dress" Subsidiary="Addis Ababa" Stores=7 Sales=76793 Inventory=136273 Returns=2433 _ERROR_=0 _N_=3
Region="Africa" Product="Sandal" Subsidiary="Addis Ababa" Stores=10 Sales=62819 Inventory=204284 Returns=1861 _ERROR_=0 _N_=4
Region="Africa" Product="Slipper" Subsidiary="Addis Ababa" Stores=14 Sales=68641 Inventory=279795 Returns=1771 _ERROR_=0 _N_=5

[/pre]

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 24 replies
  • 4920 views
  • 0 likes
  • 6 in conversation