Hello,
I am generating "if/then" clauses by concatening different variables from a dataset. I end with a variable named X whose the longest record has 928 characters. Then I export this variable X into a TXT file so I can call this file whenever I need in a data-set step:
data _null_;
set mytable;
FILE "C:\myprogram.txt" ;
PUT X;
run ;
data mysample;
set mysample;
infile "C:\myprogram.txt" ;
run;
Problem is that the export to the TXT file has broken lines after 255 characters, causing errors in the data-set step.
Does anyone knows how to avoid this issue?
Thanks
ntro
Use the LRECL option on the FILE statement to use a longer line length.
Use the LRECL option on the FILE statement to use a longer line length.
Thank you very much Tom. I feel like a newbie (which I am not but since I am a self-taught SASer, I miss some basics)
I am now using the TXT file in the data-set step:
data mysample;
set mysample;
put "C:\myprogram.txt" ; /* I wrote "infile" in my 1st post which I think is not correct */
run;
but it doesn't seem to work. The log shows thousands of "C:\myprogram.txt" lines; there is no error but the result is not what I expect.
Am I missing something (again)?
Thanks
This is the step that is WRITING the text file. Make the LRECL longer than any possible value that you want to write on any one line.
data _null_;
set mytable;
FILE "C:\myprogram.txt" lrecl=30000;
PUT X;
run ;
If you want to read it back in again make sure to also include LRECL on the INFILE statement to prevent SAS from truncating the lines on the way in.
data new_table;
INFILE "C:\myprogram.txt" lrecl=30000 truncover;
INPUT X $1000. ;
run ;
Thanks for helping Tom.
Actually I don't want to create a new variable into my new_table: I want to call and execute the code (if/then clauses) written into myprogram.txt for an existing mysample table.
Use %INCLUDE statement to pull in code from an existing text file.
%include "c:\myprogram.txt" ;
The file should contain complete statements.
We're getting closer!
I had some (red) errors in the log so I tried
%include "c:\myprogram.txt" /S2=30000;
Now the only warning I have is "truncated record" (it is in french so I cannot guarantee how it looks like in english).
I think the real solution is to fix the logic that is generating the code.
Personally I like to use a SAS data step to generate code as I can then take advantage of the power of the put statement.
For example try this little example of generating code to re-create the AGE variable from the NAME variable in SASHELP.CLASS. (it is a contrived example I know).
filename code temp;
data _null_;
set sashelp.class end=eof ;
file code ;
if _n_=1 then put 'select (name);' ;
put @3 'when (' name :$quote. ') ' age= ';' ;
if eof then put @3 'otherwise age=.;' / 'end;' ;
run;
data want ;
set sashelp.class (drop=age);
%inc code / source2;
run;
proc compare data=want compare=sashelp.class;
run;
1472 data want ;
1473 set sashelp.class (drop=age);
1474 %inc code / source2;
NOTE: %INCLUDE (level 1) file CODE is file .../#LN00236.
1475 +select (name);
1476 + when ("Alfred" ) Age=14 ;
1477 + when ("Alice" ) Age=13 ;
1478 + when ("Barbara" ) Age=13 ;
1479 + when ("Carol" ) Age=14 ;
1480 + when ("Henry" ) Age=14 ;
1481 + when ("James" ) Age=12 ;
1482 + when ("Jane" ) Age=12 ;
1483 + when ("Janet" ) Age=15 ;
1484 + when ("Jeffrey" ) Age=13 ;
1485 + when ("John" ) Age=12 ;
1486 + when ("Joyce" ) Age=11 ;
1487 + when ("Judy" ) Age=14 ;
1488 + when ("Louise" ) Age=12 ;
1489 + when ("Mary" ) Age=15 ;
1490 + when ("Philip" ) Age=16 ;
1491 + when ("Robert" ) Age=12 ;
1492 + when ("Ronald" ) Age=15 ;
1493 + when ("Thomas" ) Age=11 ;
1494 + when ("William" ) Age=15 ;
1495 + otherwise age=.;
1496 +end;
NOTE: %INCLUDE (level 1) ending.
1497 run;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.WANT has 19 observations and 5 variables.
Tom,
I added the LRECL option (%include /LRECL=2000) and it works now perfectly.
Thanks for your patience.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.