BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Helal
Fluorite | Level 6

My report has a requirement to have specific report header it the first row (Cell1) and then save th eoutput as csv shoing column labels since column names don't match the required naming convention. I am using SAS Enterprise Guide 8.3 and managed to export the variable lables rahter than names. However, I still can't get to include the header in the first cell. Here are suggested codes:

DATA _null_;
  FILE "C:\output.csv" DLM="," ;
  PUT "Consent";
  PUT;
RUN;
 
PROC EXPORT DATA=FINAL
    OUTFILE="C:\output.csv"
            DBMS=CSV
            REPLACE
            LABEL;
PUTNAMES=Yes;
RUN;

---------------------------------------------------------

The codes didn't generate any eorros but didn't add the text in the first row either. I appreciate any suggestions?

desired csv output:

First Row: Consent

Second Row: Data Column Labels (rahter names)

Third row on: Data

 

Thank you,

 

1 ACCEPTED SOLUTION

Accepted Solutions
Helal
Fluorite | Level 6

Thank you Tom. It worked like a charm.

Helal

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Delimited files with no headers are TRIVIAL to create with SAS data step (see the last step below).  And it takes only a little extra effort to generate a header row first.  You could add your extra headers line at that point.

 

First get the names and labels (if any) of the variables into a dataset.  PROC TRANSPOSE with zero input observations is a simple way to do that.

proc transpose data=FINAL(obs=0) out=names;
  var _all_;
run;

Now use that dataset of names to write your first two lines. You can use COALESCEC() to have it use the NAME when the variable does not have a LABEL attached.

data _null_;
  file  "C:\output.csv" dsd;
  length _name_ $32 _label_ $256 ;
  set names;
  if _n_=1 then put 'Consent';
  _label_=coalescec(_label_,_name_);
  put _label_ @ ;
run;

Then just run a second data step to append the actual data lines by adding the MOD option to the FILE statement.

data _null_;
  file  "C:\output.csv" dsd mod;
  set FINAL;
  put (_all_) (+0);
run;

 

Helal
Fluorite | Level 6

Thank you Tom. It worked like a charm.

Helal

Ksharp
Super User

You gotta need CSVALL destination,

but if you have a big table, this way would be very slowly.

 

data FINAL;
 set sashelp.class;
 label sex='sex  sfder';
run;

ods _all_ close;
ods csvall file='c:\temp\a\output.csv';
title;
ods text='Consent';
proc report data=final nowd ;
define _all_/display;
run;
ods csvall close;

Ksharp_0-1710815951563.png

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1470 views
  • 0 likes
  • 3 in conversation