Page 1 of 1

Export ElementAttributes to ASCII file

Posted: Tue Jan 26, 2016 10:15 pm
by wypwong
I have about 20 }ElementAttributes_xx cubes like below that I want to export to Text File, but they all have different numbers of attributes. Instead of having 20 different TI, I want to have just one. Can someone help me on how I can approach it please?

Dim1 Attribute1 Attribute2 Attribute3
A A1 A2 A3
B B1 B2 B3
C C1 C2 C3

Instead of having the ASCIIOUTPUT as
A A1
A A2
A A3
B B1
B B2
....

And I want my output just look like the cube.
A A1 A2 A3
B B1 B2 B3
C C1 C2 C3

Re: Export ElementAttributes to ASCII file

Posted: Tue Jan 26, 2016 10:32 pm
by declanr
Parameter:
psDimName

Prolog:

Code: Select all


cAttrDim = '}ElementAttributes_' | psDimName;
cExportFile = <MyFolder> | 'AttributeExport_' | psDimName | '.txt';

sExportString = 'Element Name';
iCount = 1;
iMax = DimSiz ( cAttrDim );
While ( iCount <= iMax );
	sAttrName = DimNm ( cAttrDim, iCount );
	sExportString = sExportString | ', ' | sAttrName;
	iCount = iCount + 1;
End;

AsciiOutput ( cExportFile, sExportString );

iCount = 1;
iMax = DimSiz ( psDimName );
While ( iCount <= iMax );
	sElementName = DimNm ( psDimName, iCount );
	sExportString = sElementName;
	iSubCount = 1;
	iSubMax = DimSiz ( cAttrDim );
	While ( iSubCount <= iSubMax );
		sAttrName = DimNm ( cAttrDim, iSubCount );
		sAttrType = DType ( cAttrDim, sAttrName );
		If ( sAttrType @= 'AN' );
			sAttrValue = NumberToString ( AttrN ( psDimName, sElementName, sAttrName ) );
		Else;
			sAttrValue = AttrS ( psDimName, sElementName, sAttrName );
		EndIf;
		sExportString = sExportString | ', ' | sAttrValue;
		iSubCount = iSubCount + 1;
	End;
	AsciiOutput ( cExportFile, sExportString );
	iCount = iCount + 1;
End;
	
I have only written this code in the reply box here so there may be typos that you need to correct as I haven't tested it in an editor.
It would also be sensible for you to expand the code to put quotes around each individual string; as otherwise if your attributes contain commas then it would mess up the export file (likewise you could change the delimiter to a pipe etc etc etc.)
Whatever changes you want to make it should be a good starting point.

Re: Export ElementAttributes to ASCII file

Posted: Wed Jan 27, 2016 9:36 pm
by wypwong
Declan

Thank you very much, it's very useful.

I have changed the code a little bit, as I already have a view with level 0 on the dimension.

Phyllis

Re: Export ElementAttributes to ASCII file

Posted: Wed Jan 27, 2016 10:04 pm
by declanr
Phyllis,

Thats good; it would be great if you could post your reworked solution here so that other users could benefit from it in the future.

Thanks,
Declan

Re: Export ElementAttributes to ASCII file

Posted: Thu Jan 28, 2016 1:17 pm
by wypwong
I have a view to look at level 0, so main code are done in data tab. I need to put in char(34) / double quotation mark, as I need to use the export in Qlikview, by doing the string, the system thinks it is one long text.

Code: Select all

  Data
  
## To print Header
#####################

IF( vExported @= '' );

   vExportString = pDim | char(34); 

   vHCount = 1;

   tHCount = DIMSIZ (vDim2); 

   WHILE ( vHCount <= tHCount);

      vAttrName = DIMNM (vDim2, vHCount);

      IF ( vHCount = tHCount);
         vExportString = vExportString | ', ' | char(34) | vAttrName  ;
      ELSE;
          vExportString = vExportString | ', ' | char(34) | vAttrName | char(34)  ;
      ENDIF;

      vHCount = vHCount + 1;
 
   END;

   ASCIIOutput( cExportFile, vExportString); 
  vExported ='X';
ENDIF;

#====================================#

IF( vExported @<> '' );

## To print data
#####################

vCount = 1;

tCount = DIMSIZ (vDim2); 

vExportString = V1 | char(34) ; 

WHILE ( vCount <= tCount);

   vAttrName = DIMNM (vDim2, vCount);

   vAttrType = DTYPE (vDim2, vAttrName);

   IF (vAttrType @= 'AN');
      vAttrData = NumberToString (ATTRN (pDim, V1, vAttrName)); 
   ELSE;
      vAttrData = Attrs(pDim, V1, vAttrName); 
   ENDIF;   
   

   IF ( vCount = tCount);
      vExportString = vExportString | ', ' | char(34) | vAttrData  ;
   ELSE;
      vExportString = vExportString | ', ' | char(34) | vAttrData | char(34)  ;
   ENDIF;


   vCount = vCount + 1;

END;

 ASCIIOutput( cExportFile, vExportString); 

ENDIF;