DATA Step, Macro, Functions and more

special export for numerical variable

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

special export for numerical variable

[ Edited ]

Hello expert,

Please, I want To export data to .txt  file format 

if variable =4567.345  then I want to get it  in .txt file with this format 

 

data toto;

v1=1111.12;

V2='ok';

vn=123456.756;

run;

 

.txt file

v1;v2;v3

1111,12;ok;123456,756

 

aim : go from a numerical variable  yyyyyy.aaabbb and getting yyyyyy,aaabbb

This will be for many variables.

Thank you

 

 

 

 


Accepted Solutions
Solution
‎01-11-2017 05:30 PM
Frequent Contributor
Frequent Contributor
Posts: 89

Re: spécial export for variable number

[ Edited ]

If I do this:

 

data mydata;
   input dept : $10. jan : numx. feb : numx8.2 mar comma8.3;
   datalines;
hoes 4344 35555 26664
ousewares 3777 48886 79998
pliances 5311 71223 41333
;

PROC EXPORT DATA= mydata OUTFILE= testtxtfile dbms= tab replace;
    PUTNAMES=YES;
run;

I get testtxtfile that looks like:

 

dept    jan    feb    mar
hoes    4344    355.55    26.664
ousewares    3777    488.86    79.998
pliances    5311    712.23    41.333

But then if you go look in the log where the PROC Export ran you will see this:

 

data _null_;
	%let _EFIERR_ = 0; /* set the ERROR detection macro variable */
	%let _EFIREC_ = 0;     /* clear export record count macro variable */
 	file 'TESTTXTFILE' delimiter='09'x DSD DROPOVER lrecl=32767;
     	if _n_ = 1 then        /* write column names or labels */
       		do;
         put
			"dept"
			'09'x
			"jan"
			'09'x
			"feb"
			'09'x
			"mar"
;
end;

set  MYDATA  end=EFIEOD;
	format dept $10. ;
	format jan best12. ;
	format feb best12. ;
        format mar best12. ; do; EFIOUT + 1; put dept $ @; put jan @; put feb @; put mar ; ; end; if _ERROR_ then call symputx('_EFIERR_',1); /* set ERROR detection macro variable */ if EFIEOD then call symputx('_EFIREC_',EFIOUT); run;

If you then replace your Proc Export with that code, and change

 

	format mar best12.

 to

	format mar numx8.3.

 

I get this output:

 

dept	jan	feb	mar
hoes	4344	355.55	26,664
ousewares	3777	488.86	79,998
pliances	5311	712.23	41,333

which is what I think you want.

 

Roundabout, and I'm sure there is a better way, but it gets there.

 

View solution in original post


All Replies
Solution
‎01-11-2017 05:30 PM
Frequent Contributor
Frequent Contributor
Posts: 89

Re: spécial export for variable number

[ Edited ]

If I do this:

 

data mydata;
   input dept : $10. jan : numx. feb : numx8.2 mar comma8.3;
   datalines;
hoes 4344 35555 26664
ousewares 3777 48886 79998
pliances 5311 71223 41333
;

PROC EXPORT DATA= mydata OUTFILE= testtxtfile dbms= tab replace;
    PUTNAMES=YES;
run;

I get testtxtfile that looks like:

 

dept    jan    feb    mar
hoes    4344    355.55    26.664
ousewares    3777    488.86    79.998
pliances    5311    712.23    41.333

But then if you go look in the log where the PROC Export ran you will see this:

 

data _null_;
	%let _EFIERR_ = 0; /* set the ERROR detection macro variable */
	%let _EFIREC_ = 0;     /* clear export record count macro variable */
 	file 'TESTTXTFILE' delimiter='09'x DSD DROPOVER lrecl=32767;
     	if _n_ = 1 then        /* write column names or labels */
       		do;
         put
			"dept"
			'09'x
			"jan"
			'09'x
			"feb"
			'09'x
			"mar"
;
end;

set  MYDATA  end=EFIEOD;
	format dept $10. ;
	format jan best12. ;
	format feb best12. ;
        format mar best12. ; do; EFIOUT + 1; put dept $ @; put jan @; put feb @; put mar ; ; end; if _ERROR_ then call symputx('_EFIERR_',1); /* set ERROR detection macro variable */ if EFIEOD then call symputx('_EFIREC_',EFIOUT); run;

If you then replace your Proc Export with that code, and change

 

	format mar best12.

 to

	format mar numx8.3.

 

I get this output:

 

dept	jan	feb	mar
hoes	4344	355.55	26,664
ousewares	3777	488.86	79,998
pliances	5311	712.23	41,333

which is what I think you want.

 

Roundabout, and I'm sure there is a better way, but it gets there.

 

Super Contributor
Posts: 371

Re: spécial export for variable number

Thank you.
That's exact
Trusted Advisor
Posts: 1,374

Re: special export for numerical variable

PROC Star
Posts: 7,363

Re: special export for numerical variable

You could use something like:

 

data toto;
  v1=1111.12;
  V2='ok';
  vn=123456.756;
  output;
  v1=2222.23;
  V2='notok';
  vn=654321.657;
  output;
run;

 

proc sql noprint;
  select name,name
    into :names separated by ';',
           :vars separated by ' ";" '
      from dictionary.columns
        where libname=upcase('work') and
                   memname=upcase('toto')
;
quit;

 

data _null_;
  set toto;
  file "c:\art\test.txt";
  put "&names.";
  put &vars.;
run;

 

HTH,

Art, CEO, AnalystFinder.com

 

Super Contributor
Posts: 371

Re: special export for numerical variable

@ll: Thank you all for your answers.

Great

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 193 views
  • 3 likes
  • 4 in conversation