BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
HitmonTran
Pyrite | Level 9

Hi,

 

the raw dataset is giving character values for numeric variables. So when I convert numeric values to character, different values display. I tried using "encoding=any" when reading in the data but it does not solve the problem.

 

 

col8,col9,col10 are derived variables from the numeric variables (prdvplc1, prpain1, pryn14) using put function

Capture.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Which variables in your photograph are numeric and which are character? Are we supposed to tell that from the icons? Are the icons to the right or the left of variable names?

 

If you want to create a character variable with the decoded value of the variable either use the same format in the PUT() or PUTN() function call or use the VVALUE() or VVALUEX() function.

data want;
  set have ;
  pyn14_raw = pyn14 ;
  pyn14_char1 = put(pyn14,yesno.);
  pyn14_char2 = putn(pyn14,'yesno.');
  pyn14_char3 = vvalue(pyn14);
  pyn14_char4 = vvaluex('pyn14');
run;

proc print;
  var pyn14: ;
run;

View solution in original post

12 REPLIES 12
PaigeMiller
Diamond | Level 26

col8,col9,col10 are derived variables from the numeric variables (prdvplc1, prpain1, pryn14) using put function

 

PUT always results in a character variable.

--
Paige Miller
HitmonTran
Pyrite | Level 9
but how do I retain the values of "Easy, Very Easy, Very Hard" when using put function for variable PRDVPLC1
PaigeMiller
Diamond | Level 26

@HitmonTran wrote:
but how do I retain the values of "Easy, Very Easy, Very Hard" when using put function for variable PRDVPLC1

How do you retain the values? You use variable PRDVPLC1, you don't convert to another variable.

 

or you might be better off using formats

--
Paige Miller
Tom
Super User Tom
Super User

Which variables in your photograph are numeric and which are character? Are we supposed to tell that from the icons? Are the icons to the right or the left of variable names?

 

If you want to create a character variable with the decoded value of the variable either use the same format in the PUT() or PUTN() function call or use the VVALUE() or VVALUEX() function.

data want;
  set have ;
  pyn14_raw = pyn14 ;
  pyn14_char1 = put(pyn14,yesno.);
  pyn14_char2 = putn(pyn14,'yesno.');
  pyn14_char3 = vvalue(pyn14);
  pyn14_char4 = vvaluex('pyn14');
run;

proc print;
  var pyn14: ;
run;
HitmonTran
Pyrite | Level 9
this was my logic:
col8=strip(put(prdvplc1,best.));
col9=strip(put(pryn14,best.));
col10=strip(put(prpain1,best.));
Tom
Super User Tom
Super User

@HitmonTran wrote:
this was my logic:
col8=strip(put(prdvplc1,best.));
col9=strip(put(pryn14,best.));
col10=strip(put(prpain1,best.));

So you wanted COL9 to be a character string that represents the raw value of PRYN14?  Is your question why sometimes the value contains letters?

 

Since you didn't tell SAS what width to use with the BEST format it will default to 12 bytes. If the value is can be represent exactly in 12 characters then the result will only contain digits and possibly a decimal point and/or a minus sign.  But if the value cannot then BEST will use scientific notation. So for some values your string might contain the letter E.   

 

If the value is a special missing, like .A or .Z or ._ then the value will be that character (without the period required when specifying the value in SAS code).

HitmonTran
Pyrite | Level 9
basically I want col8 to have values of ''Easy", "Very Easy", "Very Hard".
Tom
Super User Tom
Super User

That is what my first answer does. So to get those values you need to use the format that generates them in the PUT() function, not the BEST format.  If you don't know what format is attached to your existing variable to make it display that way then use the VVALUE() function instead of the PUT() function.

Shmuel
Garnet | Level 18

@HitmonTran wrote:
basically I want col8 to have values of ''Easy", "Very Easy", "Very Hard".

 Create a format by next code:

Proc format lib=work;
     value is_easy
       1  = "Very Easy"
       2 = "Easy"
       5 = "Very Hard"
  ; run;

data want;
   set have;
         length new_var $10;
         new_var = put(var , is_easy.);  /* var should be num type */
    ...
run;

 

Patrick
Opal | Level 21

@HitmonTran 

Reading through this thread I believe the bit you're missing is that variable PRDVPLC1 is already numerical and the INTERNAL values will just be numbers. The blue icon in the data grid shows you that the variable is of type numeric. Such variables can't and will never store text.

Patrick_0-1595121254730.png

You see text in the data grid because there must be a permanent format applied on variable PRDVPLC1. A format doesn't change the internal values but it changes how the values get displayed/printed.

You can always chose to change the format.

Proc Print data=<your data>; format prdvplc1 best32.; var prdvplc1; run; 

...will print the values showing you the digits and not text.

 

For creating your variable col8 you can do one of below two things:

1. To create another numerical variable.

col8=prdvplc1;

format col8 <name of format>;

2. To create a character variable containing the displayed values of the source variable

col8=put(prdvplc1, <name of format>.);

 

To get the name of the format: Run a Proc Contents on your table. That will show you the format name attached to variable prdvplc1.

 

And last but not least: Many procedures allow you to chose if you want to use the formatted or unformatted values for calculations so often there is no need to "duplicate" columns.

Shmuel
Garnet | Level 18

It is absolutely not clear what input you have and what output you want.

You had just posted the undesired output.

 

Please post next information:

1) Is the input a sas dataset or an external file

2) Is the output a report? an external file? or a new dataset?

3) If the input is a sas dataset, are those columns col8-col10 of

     a numeric type or a character type?

4) post the input value of those columns and the wanted value of each of them?

5) post your code to create the output

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 2223 views
  • 1 like
  • 5 in conversation