- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 11-02-2010 04:28 PM
(30572 views)
All,
Is there an easy way to remove decimal points from data? I have variables yvar1-yvar24 that I want to output to a text file without decimal points.
the data might be 2.12 or .801 and I'd like them to be 212 and 801. Is there any way to take out the decimal places either before or during the export? Thanks.
Is there an easy way to remove decimal points from data? I have variables yvar1-yvar24 that I want to output to a text file without decimal points.
the data might be 2.12 or .801 and I'd like them to be 212 and 801. Is there any way to take out the decimal places either before or during the export? Thanks.
25 REPLIES 25
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You could use the compress function, e.g. yvar1=compress(yvar1,'.')
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Presuming yours are SAS NUMERIC type variables, you can use a SAS assignment statement to create a uniquely-named SAS CHARACTER type variable using the COMPRESS function, as mentioned, but with a different SAS variable. And, to avoid the SAS NOTE about conversion, use the PUT(,BEST.) in your assignment surrounded by the COMPRESS.
Scott Barry
SBBWorks, Inc.
Suggested Google advanced search argument, this topic / post:
convert numeric character variable site:sas.com
Scott Barry
SBBWorks, Inc.
Suggested Google advanced search argument, this topic / post:
convert numeric character variable site:sas.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Ok, first off thanks. Second, the compress function must be used with character variables? Can I use a put statement to convert yvar1-yvar24 at the same time to character variables or would I need to do them indiviually?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
of course you could compress after converting individually to char type.
converted = compress( put( yVar1, best10.), '.' );
or
converted = compress( vvalue(vYar1 ), '.' ) ;[pre]
175 data ;
176 retain yVar1 123.45 yVar2 456.789 ;
177 converted1 = compress( put( yVar1, best10.), '.' );
178 converted2 = compress( vvalue(yVar2 ), '.' ) ;
179 put (_all_)(=/);
180 run;
yVar1=123.45
yVar2=456.789
converted1=12345
converted2=456789
NOTE: The data set WORK.DATA3 has 1 observations and 4 variables.
NOTE: DATA statement used (Total process time):[/pre]
Trying to avoid that is why I suggested compressing periods out of the line just before you release it. Here is a saslog to demonstrate[pre]287 data test_data ;
288 input yVar1 - yVar24 ;
289 format yVar: best18. ;
290 list;cards ;
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
291 12 123.4 2345.678 765.2 45678.1234 456 54678.456 1.45678 .3456 2345 12345 9843 12345678 5432.54 3456
101 7889045 .2345 23456789.6789 1234567890.32 23456.1234 123.3456 .345 3456.2345 .4567 1234567890.32 23
201 456.1234 123.3456 .345 3456.2345 .4567
NOTE: The data set WORK.TEST_DATA has 1 observations and 24 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
292 ;
293
294 data _null_ ;
295 file 'no_dots.txt' dsd dlm='|' lrecl=10000 ; * pipe delimited output ;
296 set ; * load data ;
297 put (yVar1-yVar20)(:) @ ; * prepare and hold output line ;
298 _file_ = compress( _file_, '.' ) ; * remove dots from line ;
299 put ; * release line ;
300 run ;
NOTE: The file 'no_dots.txt' is:
File Name=C:\Applications\UK.Liquidity2\no_dots.txt,
RECFM=V,LRECL=10000
NOTE: 1 record was written to the file 'no_dots.txt'.
The minimum record length was 150.
The maximum record length was 150.
NOTE: There were 1 observations read from the data set WORK.TEST_DATA.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
301 data _null_ ;
302 infile 'no_dots.txt' lrecl=10000 ;
303 input ;
304 list ;
305 run ;
NOTE: The infile 'no_dots.txt' is:
File Name=C:\Applications\UK.Liquidity2\no_dots.txt,
RECFM=V,LRECL=10000
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
1 12|1234|2345678|7652|456781234|456|54678456|145678|03456|2345|12345|9843|12345678|543254|34567889045
101 |02345|234567896789|123456789032|234561234|1233456 150
NOTE: 1 record was read from the infile 'no_dots.txt'.
The minimum record length was 150.
The maximum record length was 150.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds[/pre]
I applied bold for the step which outputs the data and removes the dots
converted = compress( put( yVar1, best10.), '.' );
or
converted = compress( vvalue(vYar1 ), '.' ) ;[pre]
175 data ;
176 retain yVar1 123.45 yVar2 456.789 ;
177 converted1 = compress( put( yVar1, best10.), '.' );
178 converted2 = compress( vvalue(yVar2 ), '.' ) ;
179 put (_all_)(=/);
180 run;
yVar1=123.45
yVar2=456.789
converted1=12345
converted2=456789
NOTE: The data set WORK.DATA3 has 1 observations and 4 variables.
NOTE: DATA statement used (Total process time):[/pre]
Trying to avoid that is why I suggested compressing periods out of the line just before you release it. Here is a saslog to demonstrate[pre]287 data test_data ;
288 input yVar1 - yVar24 ;
289 format yVar: best18. ;
290 list;cards ;
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
291 12 123.4 2345.678 765.2 45678.1234 456 54678.456 1.45678 .3456 2345 12345 9843 12345678 5432.54 3456
101 7889045 .2345 23456789.6789 1234567890.32 23456.1234 123.3456 .345 3456.2345 .4567 1234567890.32 23
201 456.1234 123.3456 .345 3456.2345 .4567
NOTE: The data set WORK.TEST_DATA has 1 observations and 24 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
292 ;
293
294 data _null_ ;
295 file 'no_dots.txt' dsd dlm='|' lrecl=10000 ; * pipe delimited output ;
296 set ; * load data ;
297 put (yVar1-yVar20)(:) @ ; * prepare and hold output line ;
298 _file_ = compress( _file_, '.' ) ; * remove dots from line ;
299 put ; * release line ;
300 run ;
NOTE: The file 'no_dots.txt' is:
File Name=C:\Applications\UK.Liquidity2\no_dots.txt,
RECFM=V,LRECL=10000
NOTE: 1 record was written to the file 'no_dots.txt'.
The minimum record length was 150.
The maximum record length was 150.
NOTE: There were 1 observations read from the data set WORK.TEST_DATA.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
301 data _null_ ;
302 infile 'no_dots.txt' lrecl=10000 ;
303 input ;
304 list ;
305 run ;
NOTE: The infile 'no_dots.txt' is:
File Name=C:\Applications\UK.Liquidity2\no_dots.txt,
RECFM=V,LRECL=10000
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
1 12|1234|2345678|7652|456781234|456|54678456|145678|03456|2345|12345|9843|12345678|543254|34567889045
101 |02345|234567896789|123456789032|234561234|1233456 150
NOTE: 1 record was read from the infile 'no_dots.txt'.
The minimum record length was 150.
The maximum record length was 150.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds[/pre]
I applied bold for the step which outputs the data and removes the dots
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Peter, that is great, thank you very much. My only problem now is that the compression removes the ending zeros in the data. Is there a way to prevent that from happening?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
well,
that implies your data are already formatted - so,
remove that statement
format yVar: best18. ;
that implies your data are already formatted - so,
remove that statement
format yVar: best18. ;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Well, I am importing the data from an excel spreadsheet. I know the actual data, meaning yvar1-yvar24, is numeric. I don't have any statement defining that, they imort that way. Are you saying I should use the format you provided? I've got everything else but this darn decimal point thing. Very frustrating.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I'm having trouble converting from numeric to character for yvar1-yvar24. I need to keep 3 implied decimal places.
For example, if one data point was 0.850 I need to it to be 0850 or another example is 1.250 I'd need it to be 1250, implying 3 decimal places. yvar1-yvar24 are imported (using the import wizard, as numeric) converting to character is required to use compress()?
For example, if one data point was 0.850 I need to it to be 0850 or another example is 1.250 I'd need it to be 1250, implying 3 decimal places. yvar1-yvar24 are imported (using the import wizard, as numeric) converting to character is required to use compress()?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
easy
proc format
allows you to define how to present numbers
it even allows you to multiply by 1000
and by 100 in another format.
If you have a consistent requirement to show the yVar1-24 in the same format [pre]proc format ;
picture millis(round)
/* missing */ . = ' '
-9.999 - -0 = '00000'(mult=1000 prefix='-')
0 - 9.999 = '00000'(mult=1000)
other = [best5.]
;
run ;[/pre]%put testing millis %sysfunc( putn( 100/81, millis )) full precision %sysfunc( putn( 100/81, best16 )) ;
%put testing millis %sysfunc( putn(-100/81, millis )) full precision %sysfunc( putn(-100/81, best16 )) ;
%put testing millis %sysfunc( putn( 1, millis )) full precision %sysfunc( putn( 1, best16 )) ;
%put testing millis %sysfunc( putn(-1, millis )) full precision %sysfunc( putn(-1, best16 )) ;
Use the millis. format like[pre]data _null_ ;
file 'your report.txt' lrecl=10000 ;
set your.data ;
format yVar1 - yVar24 millis. ;
put yVar1 -yVar24 ;
run ;[/pre]
proc format
allows you to define how to present numbers
it even allows you to multiply by 1000
and by 100 in another format.
If you have a consistent requirement to show the yVar1-24 in the same format [pre]proc format ;
picture millis(round)
/* missing */ . = ' '
-9.999 - -0 = '00000'(mult=1000 prefix='-')
0 - 9.999 = '00000'(mult=1000)
other = [best5.]
;
run ;[/pre]%put testing millis %sysfunc( putn( 100/81, millis )) full precision %sysfunc( putn( 100/81, best16 )) ;
%put testing millis %sysfunc( putn(-100/81, millis )) full precision %sysfunc( putn(-100/81, best16 )) ;
%put testing millis %sysfunc( putn( 1, millis )) full precision %sysfunc( putn( 1, best16 )) ;
%put testing millis %sysfunc( putn(-1, millis )) full precision %sysfunc( putn(-1, best16 )) ;
Use the millis. format like[pre]data _null_ ;
file 'your report.txt' lrecl=10000 ;
set your.data ;
format yVar1 - yVar24 millis. ;
put yVar1 -yVar24 ;
run ;[/pre]
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
When trying to use the millis format it tells me it is not found or could not be loaded. I am running SAS 9.1, is this not a part of 9.1?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
that is what the proc format code is for
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I see. You created your own format. Let me play with that and I'll report back. Thanks Peter.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Peter, that does everything I need except for it drops the leading zeros for the observations that are less than one. I.E.
0.250 becomes 250 while I need it to look like 0250. The values greater than one like 1.350 look like 1350 and are perfect. Your format works great for removing the decimal point. I would like to learn all this stuff myself, I just need to keep plugging away to gain the knowledge. Maybe once I figure out this project you can run through that format code with a quick explanation?
0.250 becomes 250 while I need it to look like 0250. The values greater than one like 1.350 look like 1350 and are perfect. Your format works great for removing the decimal point. I would like to learn all this stuff myself, I just need to keep plugging away to gain the knowledge. Maybe once I figure out this project you can run through that format code with a quick explanation?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Given you are interested in a "formatted value", it appears you will need to run through your SAS variable list using PUTN (rather than PUT as I suggested using the BEST. format) along with the return-value from VFORMAT, in order to get your "numeric" data accurately represented as "character-string" data but with any intervening decimal points removed.
Scott Barry
SBBWorks, Inc.
Suggested Google advanced search arguments, this topic / post:
putn function site:sas.com
vformat function site:sas.com
Scott Barry
SBBWorks, Inc.
Suggested Google advanced search arguments, this topic / post:
putn function site:sas.com
vformat function site:sas.com