Hi Guys,
How do I put variable names on top of the ASCII data file I am creating?
Any insight would be much appreciated.
Thank you.
Patrick
data ASCII;
set final.merged_data;
NEW_BP=LOG(BP/LAG(BP));
NEW_CD=LOG(CD/LAG(CD));
NEW_DM=LOG(DM/LAG(DM));
NEW_JY=LOG(JY/LAG(JY));
NEW_SF=LOG(SF/LAG(SF));
file '/folders/myfolders/sasuser.v94/ascii.txt';
drop DAY;
drop BP;
drop CD;
drop DM;
drop JY;
drop SF;
format DATE MMDDYY10. NEW_BP NEW_CD NEW_DM NEW_JY NEW_SF;
put DATE MMDDYY10. NEW_BP NEW_CD NEW_DM NEW_JY NEW_SF;
run;
Old school:
data ASCII;
set final.merged_data;
NEW_BP=LOG(BP/LAG(BP));
NEW_CD=LOG(CD/LAG(CD));
NEW_DM=LOG(DM/LAG(DM));
NEW_JY=LOG(JY/LAG(JY));
NEW_SF=LOG(SF/LAG(SF));
file '/folders/myfolders/sasuser.v94/ascii.txt';
drop DAY;
drop BP;
drop CD;
drop DM;
drop JY;
drop SF;
format DATE MMDDYY10. NEW_BP NEW_CD NEW_DM NEW_JY NEW_SF;
If _n_ = 1 then put "Date" +5 "New_BP" +5 "New_CD" +5 "New_JY" +5 "New_SF";
put DATE MMDDYY10. NEW_BP NEW_CD NEW_DM NEW_JY NEW_SF;
run;
Adjust the column movements, the +5, to align. You might want to consider using column assignments in the put to align data in columns as well, in which case use the same column assignments
Hi. An easy way is to just print the data set rather than using a PUT statement in a data setp. For example, use ODS CSV and chamhe the delimiter to a space (same thing you are doing in your PUT statement, you could always add the date format in PROC PRINT) ...
ods csv file='z:\class.txt' options(delimiter=' ');
proc print data=sashelp.class noobs;
run;
ods csv close;
portion of CLASS.TXT ...
"Name" "Sex" "Age" "Height" "Weight"
"Alfred" "M" 14 69.0 112.5
"Alice" "F" 13 56.5 84.0
"Barbara" "F" 13 65.3 98.0
"Carol" "F" 14 62.8 102.5
Just as an alternative:
proc sql noprint;
select NAME
into :HEAD separated by ","
from DICTIONARY.COLUMNS
where LIBNAME="SASHELP"
and MEMNAME="CARS";
quit;
data _null_;
set sashelp.cars;
file "c:\xyz.csv" dlm=",";
if _n_=1 then put "&HEAD.";
else put make model type;
run;
Note, the datastep method does allow you to do processing, and other nice things whilst exporting.
Old school:
data ASCII;
set final.merged_data;
NEW_BP=LOG(BP/LAG(BP));
NEW_CD=LOG(CD/LAG(CD));
NEW_DM=LOG(DM/LAG(DM));
NEW_JY=LOG(JY/LAG(JY));
NEW_SF=LOG(SF/LAG(SF));
file '/folders/myfolders/sasuser.v94/ascii.txt';
drop DAY;
drop BP;
drop CD;
drop DM;
drop JY;
drop SF;
format DATE MMDDYY10. NEW_BP NEW_CD NEW_DM NEW_JY NEW_SF;
If _n_ = 1 then put "Date" +5 "New_BP" +5 "New_CD" +5 "New_JY" +5 "New_SF";
put DATE MMDDYY10. NEW_BP NEW_CD NEW_DM NEW_JY NEW_SF;
run;
Adjust the column movements, the +5, to align. You might want to consider using column assignments in the put to align data in columns as well, in which case use the same column assignments
Thank you!
Hi. You got a bunch of answers. Great. How about some 'NEW USER' stuff ...
Why all the DROP statements? How about ...
drop DAY BP CD DM JY SF;
Also, not sure what this does ...
format DATE MMDDYY10. NEW_BP NEW_CD NEW_DM NEW_JY NEW_SF;
All it would do is remove the formats from all the NEW_ variables since there is no format used at the end of the format for those variables. But you created those variables within the data step and they have no formats associated with them.
Last, you have DATE in a FORMAT statement and you also have a format for DATE in the PUT statement. One of those is enough. If a variable has been assigned a format (in this case MMDDYY10.), it will be formatted when you use a PUT statement (no need to repeat the format).
Thanks for the tips and advice Mike
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.