10-31-2013 07:44 AM
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:
FILE "C:\myprogram.txt" ;
infile "C:\myprogram.txt" ;
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?
10-31-2013 11:18 AM
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:
put "C:\myprogram.txt" ; /* I wrote "infile" in my 1st post which I think is not correct */
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)?
10-31-2013 11:28 AM
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.
FILE "C:\myprogram.txt" lrecl=30000;
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.
INFILE "C:\myprogram.txt" lrecl=30000 truncover;
INPUT X $1000. ;
10-31-2013 11:57 AM
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.
10-31-2013 01:13 PM
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).
10-31-2013 02:04 PM
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;
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;' ;
data want ;
set sashelp.class (drop=age);
%inc code / source2;
proc compare data=want compare=sashelp.class;
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=.;
NOTE: %INCLUDE (level 1) ending.
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.WANT has 19 observations and 5 variables.
Need further help from the community? Please ask a new question.