BookmarkSubscribeRSS Feed
oht
Calcite | Level 5 oht
Calcite | Level 5


Hi everyone.

This seems to be a simple request but I got stuck and need some help . I have a datasets with 756 variables and some of them are numeric field type=1. Is there any already make routine that I can convert all the numeric var into character var ?

Thanks for all your help

21 REPLIES 21
HK_EndeavourForever
Fluorite | Level 6

Hello Oht,

Do you use any layout to read the SAS dataset with 756 variables in your sas program?

Thank You!

oht
Calcite | Level 5 oht
Calcite | Level 5

Hi

I inherated the SAS dataset without layout. The proc contents will get the layout. The problem is when I writing the data out to a flat file. What kind of field length should I define these numeric values. If I use the default BEST12. and the value happened to be more that 12 then SAS will convert these number into scientific lile 123E2 and I try to avoid this happening. Since the length function cannot determie the true length of the numeric field, the other alternative is to convert all of them into char and use the lengh func to determine the max length of each field.  Do you know of any macro that can convert the num var to char ?

Thanks

Astounding
PROC Star

So why use best12.?  This would be working code:

put var1 : best16.  +(-1) '|'   var2 : best16.  +(-1) '|'  var3 best16.;

The colon indicates that values get written in scanning mode (no leading blanks, one trailing blank).  The +(-1) repositions the pointer back a space, so the trailing blank gets overwritten with the delimiter.

Good luck.

oht
Calcite | Level 5 oht
Calcite | Level 5

WOW, thanks for all your quick responses. I thinks I have enough info to go on..

Again, thanks for all your help. Really make me feel good to be a SAS user !

art297
Opal | Level 21

Then give formal credit to those who helped you by marking their posts as either being the correct answer or at least having been helpful.

Please don't mark my post either way as I didn't provide anything toward this thread.

Peter_C
Rhodochrosite | Level 12

convert all numerics to character with

put _numeric_ ;

default formats will be used, the list will be blank delimited.

If that suits, but you want to have them all in a string, use

length collection $32767 ;

put; *clear the buffer;

format _numeric_ 32. ;

put _numeric_ @ ;

collection = _file_ ;

_file_ = ' ' ;

put @1 @ ;

oht
Calcite | Level 5 oht
Calcite | Level 5

Hi Peter,

Thanks for your interesting idea. However, when I tested your code, there is an error.

 

23 data have;

24 input id $ sex $ a b c d e ;

25 cards;

NOTE: The data set WORK.HAVE has 1 observations and 7 variables.

NOTE: DATA statement used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds

 

27 ;

28 data test;

29 length collection $32767 ;

30 set have ;

31 put; *clear the buffer;

32 put _numeric_ @ ;

ERROR: Cannot use _numeric_ as a variable name.

33 collection = _file_ ;

34 _file_ = ' ' ;

35 put @1 @ ;

36 run;

NOTE: The SAS System stopped processing this step because of errors.

Do you know what cause this error ?

Thanks

art297
Opal | Level 21

The cause of the error is described at: http://www.sascommunity.org/wiki/PUT_Statement_with_ALL

You can get around it by using:

data have;

input id $ sex $ a b c d e ;

cards;

1 M 1 2 3 4 5

;

data test;

length collection $32767 ;

set have ;

put; *clear the buffer;

put (_numeric_) (+0) @ ;

collection = _file_ ;

_file_ = ' ' ;

put @1 @ ;

run;

MikeZdeb
Rhodochrosite | Level 12

hi ... I think that you can get away with fewer statements (you'll end up with one record written to the LOG) ... COLLECTION ends up with the same length as _FILE_ and that defaults to 32767 so maybe a LENGTH statement is appropriate ...

data have;

input (id sex) (:$1.) a b c d e @@;

cards;

1 M 1 2 3 4 5 2 F 9 9 9 9 9 3 M 8 8 8 8 8 1 M 1 2 3 4 5

2 F 9 9 9 9 9 3 M 8 8 8 8 8 3 M 8 8 8 8 8 4 Q 9 9 9 9 9

;

data test;

set have;

put @1 (_numeric_) (:) @ ;

collection = _file_ ;

run;

LOG ...

441  data test;

442  set have;

443  put @1 (_numeric_) (:) @ ;

444  collection = _file_ ;

445  run;

9 9 9 9 9

NOTE: There were 8 observations read from the data set WORK.HAVE.

NOTE: The data set WORK.TEST has 8 observations and 8 variables.

data set ...

id    sex    a    b    c    d    e    collection

1      M     1    2    3    4    5    1 2 3 4 5

2      F     9    9    9    9    9    9 9 9 9 9

3      M     8    8    8    8    8    8 8 8 8 8

1      M     1    2    3    4    5    1 2 3 4 5

2      F     9    9    9    9    9    9 9 9 9 9

3      M     8    8    8    8    8    8 8 8 8 8

3      M     8    8    8    8    8    8 8 8 8 8

4      Q     9    9    9    9    9    9 9 9 9 9

oht
Calcite | Level 5 oht
Calcite | Level 5

Thanks Mike and Peter,

This is a very interesting idea. VERY COOL ! Even I don't use for this apps but definitely save for future reference..

oht
Calcite | Level 5 oht
Calcite | Level 5

Hi Mike and Peter,

One more question, if I do not want the number to combine in a single variable collection and want to convert all the variables from numeric to char, how would i apply this _numeric_ to get that that done ?

Thanks

MikeZdeb
Rhodochrosite | Level 12

hi ... one way (using an idea based on a recent PGSTATS posting) ...

data have;

input (id sex) (:$1.) a b c d e @@;

cards;

1 M 1 2 3 4 5 2 F 9 9 9 9 9 3 M 8 8 8 8 8 1 M 1 2 3 4 5

2 F 9 9 9 9 9 3 M 8 8 8 8 8 3 M 8 8 8 8 8 4 Q 9 9 9 9 9

;

data test;

input @;

do until (done);

  set have end=done;

  put @1 (_numeric_) (:) @ ;

  _infile_ = _file_;

  input @1 (x1-x5) (:$1.) @;

  output;

end;

datalines;

1

;

Alphabetic List of Variables and Attributes

#    Variable    Type    Len

3    a           Num       8

4    b           Num       8

5    c           Num       8

6    d           Num       8

7    e           Num       8

1    id          Char      1

2    sex         Char      1

8    x1          Char      1

9    x2          Char      1

10    x3          Char      1

11    x4          Char      1

12    x5          Char      1

id    sex    a    b    c    d    e    x1    x2    x3    x4    x5

1      M     1    2    3    4    5    1     2     3     4     5

2      F     9    9    9    9    9    9     9     9     9     9

3      M     8    8    8    8    8    8     8     8     8     8

1      M     1    2    3    4    5    1     2     3     4     5

2      F     9    9    9    9    9    9     9     9     9     9

3      M     8    8    8    8    8    8     8     8     8     8

3      M     8    8    8    8    8    8     8     8     8     8

4      Q     9    9    9    9    9    9     9     9     9     9

PGStats
Opal | Level 21

Wow Mike!

_infile_ = _file_;

Do not show this to minors! :smileygrin:

PG

PG
MikeZdeb
Rhodochrosite | Level 12

hi ... when I saw your idea about a week ago, I though that it might be a neat way to recode a lot of variables (possibly an alternative to an array and a loop)...

proc format;

value x 10 = '0' other='1';

run;

data x;

input x1-x10;

datalines;

10 20 30 10 10 10 10 10 10 88

10 10 10 11 12 10 13 15 16 99

99 99 99 99 99 99 99 99 10 10

;

data y;

input @;

do until(done);

   set x end=done;

   put @1 (x1-x10) (x.) @;

   _infile_ = _file_;

   input @1 (x1-x10) (1.) @;

   output;

end;

datalines;

dummy

;

x1    x2    x3    x4    x5    x6    x7    x8    x9    x10

0     1     1     0     0     0     0     0     0     1

0     0     0     1     1     0     1     1     1     1

1     1     1     1     1     1     1     1     0     0

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 21 replies
  • 2648 views
  • 4 likes
  • 9 in conversation