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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

6 REPLIES 6
MikeZdeb
Rhodochrosite | Level 12

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

 

 

ballardw
Super User

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

 

pawaterman
Fluorite | Level 6

Thank you!

MikeZdeb
Rhodochrosite | Level 12

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

pawaterman
Fluorite | Level 6

Thanks for the tips and advice Mike Smiley Happy

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
  • 6 replies
  • 1140 views
  • 4 likes
  • 4 in conversation