DATA Step, Macro, Functions and more

Use macro to create txt file

Accepted Solution Solved
Reply
Contributor
Posts: 67
Accepted Solution

Use macro to create txt file

Hello,

I have a big SAS dataset that has 5m records with 160 variables. I want to create a txt file and then create a zip file of it so I can send to others.

Here is what I did:

1. Created a macro variable for txt file header called &flat_head. Here is how it look like:  ID|F_name|L_name|Age|DOB|Zip_code|City|........

2. Created a macro variable &putvar for put step: ID F_name L_name Age DOB Zip_code City|

3. Created a txt file using these two macro variables.

However, I'm not sure which step I did wrong. program did not work.

Any suggestions on this?

Thank you in advance.

proc contents data = file noprint

    out = test(keep=name rename=(name=varname));

run;

Proc sql noprint;

  select varname into: Flat_head separated by '|'

  from test;

%put flat_head=&flat_head.; 

Data test2;

  Set test END=LAST;

  Length putvar $1000;

  IF _N_ EQ 1 Then  putvar='';

  putvar=Trim(putvar)||' '||Trim(varname);

  Retain putvar;

  IF LAST Then Call Symput('putvar', Trim(putvar));

Run;

%Put putvar=&putvar.;

data _NULL_;

  SET file END=EOF;

  FILE FLATOUT  DELIMITER='|' DSD;

IF _N_=1 THEN PUT "&Flat_head.";

  PUT &putvar.;

  IF EOF THEN PUT "TOTAL NUMBER OF RECORDS = " _N_;

RUN;


Accepted Solutions
Solution
‎05-16-2015 12:35 AM
Super User
Posts: 17,819

Re: Use macro to create txt file

Where did you define flatout and there is no quit for your sql statement.

Also, why the data step for the second macro variable not another sql step?

proc contents data = file noprint

    out = test(keep=name rename=(name=varname));

run;

Proc sql noprint;

  select varname into: Flat_head separated by '|'

  from test;

  select varname into: putvar separated by " "

from test;

quit;

%put flat_head=&flat_head.; 

%put put_var=&put_var.;

data _NULL_;

  SET file END=EOF;

  FILE 'C:\temp\test.txt'  DELIMITER='|' DSD;

  IF _N_=1 THEN PUT "&Flat_head.";

  PUT &putvar.;

  IF EOF THEN PUT "TOTAL NUMBER OF RECORDS = " _N_;

RUN;

View solution in original post


All Replies
Solution
‎05-16-2015 12:35 AM
Super User
Posts: 17,819

Re: Use macro to create txt file

Where did you define flatout and there is no quit for your sql statement.

Also, why the data step for the second macro variable not another sql step?

proc contents data = file noprint

    out = test(keep=name rename=(name=varname));

run;

Proc sql noprint;

  select varname into: Flat_head separated by '|'

  from test;

  select varname into: putvar separated by " "

from test;

quit;

%put flat_head=&flat_head.; 

%put put_var=&put_var.;

data _NULL_;

  SET file END=EOF;

  FILE 'C:\temp\test.txt'  DELIMITER='|' DSD;

  IF _N_=1 THEN PUT "&Flat_head.";

  PUT &putvar.;

  IF EOF THEN PUT "TOTAL NUMBER OF RECORDS = " _N_;

RUN;

Contributor
Posts: 67

Re: Use macro to create txt file

Thanks Reeza, It works now.

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 210 views
  • 1 like
  • 2 in conversation