I have a text file with a list of codes, with the '\' character as the delimiter. Each record has variable length and variable number of codes, and the codes are variable length. I'm outputting each code into its own observation. Here is a sample of three records:
\UK\CL\FCL\RCL\UCL\CO2\IGAP\CA\RCA\UCA\MG\RMG\UMG\P\RP\UP\URIC\RUR\UUR
\NBIL\BILT\FBIL\BC\BU\CHOL\FCHO\TRIG\FTRI\HDL\LDLC\LDLD\TP\FPRT\TPEL\RPR
#PREL\UPRO\TPELR\SFPR\ALB\RMA\RAMA\UMA\UMAR\FLEX\FALBC\MYOG\TRAN
I'm using DLM='\' and FLOWOVER and OUTPUT getting the desired output SAS dataset. However, I have a situation where the Code value may be wrapped onto the following input row. See the red text above. The value of Code is actually 'RPR#PREL', however, what I'm getting are two discreet observations for Code:
RPR
#PREL
It's like the LF is playing the role of a defacto delimiter.
Thoughts on how I can get the desired value into the variable?
Thanks for any ideas!
Something like below appears to work.
filename mysource temp;
data _null_;
file mysource;
put '\UK\CL\FCL\RCL\UCL\CO2\IGAP\CA\RCA\UCA\MG\RMG\UMG\P\RP\UP\URIC\RUR\UUR';
put '\NBIL\BILT\FBIL\BC\BU\CHOL\FCHO\TRIG\FTRI\HDL\LDLC\LDLD\TP\FPRT\TPEL\RPR';
put '#PREL\UPRO\TPELR\SFPR\ALB\RMA\RAMA\UMA\UMAR\FLEX\FALBC\MYOG\TRAN';
put '\NBIL\BILT\FBIL\BC\BU\CHOL\FCHO\TRIG\FTRI\HDL\LDLC\LDLD\TP\FPRT\TPEL\RPR';
put '#PREL\UPRO\TPELR\SFPR\ALB\RMA\RAMA\UMA\UMAR\FLEX\FALBC\MYOG\TRAN';
run;
data test;
infile mysource termstr=crlf dlm='\' recfm=n lrecl=256;
input code :$20. @@;
code=compress(code,'0a'x);
output;
run;
proc print data=test;
run;
This seems to work.
filename F "&wdir\t.txt";
data _null_;
file F;
put '\UK\CL\FCL\RCL\UCL\CO2\IGAP\CA\RCA\UCA\MG\RMG\UMG\P\RP\UP\URIC\RUR\UUR';
put '\NBIL\BILT\FBIL\BC\BU\CHOL\FCHO\TRIG\FTRI\HDL\LDLC\LDLD\TP\FPRT\TPEL\RPR';
put '#PREL\UPRO\TPELR\SFPR\ALB\RMA\RAMA\UMA\UMAR\FLEX\FALBC\MYOG\TRAN';
put '\NBIL\BILT\FBIL\BC\BU\CHOL\FCHO\TRIG\FTRI\HDL\LDLC\LDLD\TP\FPRT\TPEL\RPR';
put '#PREL\UPRO\TPELR\SFPR\ALB\RMA\RAMA\UMA\UMAR\FLEX\FALBC\MYOG\TRAN';
run;
data WANT;
length ROW NEXT $256 STR $8;
retain NEXT STR;
infile F end=LASTREC;
input ;
ROW =NEXT;
NEXT=_infile_;
if _N_=1 then return;
WAIT = ( first(NEXT) ne '\' );
NBVAL=countw(ROW,'\');
do RECNO = 0 to LASTREC;
do VALNO = 1 to NBVAL ;
%* Read ROW, attach previous value if needed.;
if first(ROW) ne '\' and VALNO=1 then STR=catt(STR,scan(ROW, VALNO, '\'));
else STR = scan(ROW, VALNO, '\');
%* Save if value is complete;
if ^( VALNO=NBVAL & WAIT) then output;
end;
if LASTREC then do;
ROW =NEXT;
NBVAL=countw(ROW,'\');
WAIT =0;
end;
end;
run;
STR |
UK |
CL |
FCL |
RCL |
UCL |
CO2 |
IGAP |
CA |
RCA |
UCA |
MG |
RMG |
UMG |
P |
RP |
UP |
URIC |
RUR |
UUR |
NBIL |
BILT |
FBIL |
BC |
BU |
CHOL |
FCHO |
TRIG |
FTRI |
HDL |
LDLC |
LDLD |
TP |
FPRT |
TPEL |
RPR#PREL |
UPRO |
TPELR |
SFPR |
ALB |
RMA |
RAMA |
UMA |
UMAR |
FLEX |
FALBC |
MYOG |
TRAN |
NBIL |
BILT |
FBIL |
BC |
BU |
CHOL |
FCHO |
TRIG |
FTRI |
HDL |
LDLC |
LDLD |
TP |
FPRT |
TPEL |
RPR#PREL |
UPRO |
TPELR |
SFPR |
ALB |
RMA |
RAMA |
UMA |
UMAR |
FLEX |
FALBC |
MYOG |
TRAN |
Something like below appears to work.
filename mysource temp;
data _null_;
file mysource;
put '\UK\CL\FCL\RCL\UCL\CO2\IGAP\CA\RCA\UCA\MG\RMG\UMG\P\RP\UP\URIC\RUR\UUR';
put '\NBIL\BILT\FBIL\BC\BU\CHOL\FCHO\TRIG\FTRI\HDL\LDLC\LDLD\TP\FPRT\TPEL\RPR';
put '#PREL\UPRO\TPELR\SFPR\ALB\RMA\RAMA\UMA\UMAR\FLEX\FALBC\MYOG\TRAN';
put '\NBIL\BILT\FBIL\BC\BU\CHOL\FCHO\TRIG\FTRI\HDL\LDLC\LDLD\TP\FPRT\TPEL\RPR';
put '#PREL\UPRO\TPELR\SFPR\ALB\RMA\RAMA\UMA\UMAR\FLEX\FALBC\MYOG\TRAN';
run;
data test;
infile mysource termstr=crlf dlm='\' recfm=n lrecl=256;
input code :$20. @@;
code=compress(code,'0a'x);
output;
run;
proc print data=test;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.