DATA Step, Macro, Functions and more

Adding Variables to ASCII data file

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Adding Variables to ASCII data file

[ Edited ]

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;


Accepted Solutions
Solution
‎10-29-2015 11:33 AM
Super User
Posts: 11,343

Re: Adding Variables to ASCII data file

Posted in reply to pawaterman

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

 

View solution in original post


All Replies
Valued Guide
Posts: 765

Re: Adding Variables to ASCII data file

Posted in reply to pawaterman

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

Super User
Super User
Posts: 7,942

Re: Adding Variables to ASCII data file

Posted in reply to pawaterman

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.

 

 

 

Solution
‎10-29-2015 11:33 AM
Super User
Posts: 11,343

Re: Adding Variables to ASCII data file

Posted in reply to pawaterman

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

 

Occasional Contributor
Posts: 9

Re: Adding Variables to ASCII data file

Thank you!

Valued Guide
Posts: 765

Re: Adding Variables to ASCII data file

[ Edited ]
Posted in reply to pawaterman

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).

Occasional Contributor
Posts: 9

Re: Adding Variables to ASCII data file

Thanks for the tips and advice Mike Smiley Happy

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 324 views
  • 4 likes
  • 4 in conversation