BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Lapis Lazuli | Level 10

Hi guys, 

I have a list of variables in my DB (I received survey data) whose values are words of sentences but are formatted as numeric. 

 

For example: the variable "Syndrome_1" has values = Weight gain; 

                         the variable "Syndrome_2" has values = other clinical signs; 

 

the format is Num; len=8; informat= 8; 

 

I tried to convert them into Char but without success. I used Syndrome_1_N= put(Syndrome_1, 8.). 

The resulting variable is empty.

 

Can anyone help me please?

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
D_Dunlap
SAS Employee

Hello @NewUsrStat ,

If I'm understanding you correctly, the underlying data is numeric, but a user-defined format has been created to display the data as character, in which case, the underlying data (X and Y) are still stored as numeric, but the format is like icing on the cake, it makes the data prettier and more understandable. 

 

However, if you want to create a character version of the variables from the underlying table, this is possible. Since you already have a user-defined FORMAT, you can take advantage of that and use it in the PUT function to convert the numeric values to character values. 

 

There are two possible scenarios I can think of her in regard to converting the numeric values to characters.

 

Example 1: You want to create two new character variables with the different names from the original variables X and Y based on the user-defined format and store both the numeric form and character form of the variables in WORK.SAMPLE1.

 

Example 2: You want to convert X and Y to character variables and assign the character values base on the user-defined format. In which case, you have to RENAME X and Y initially because variables in SAS can't be both numeric and character at the same time. Then you can use an assignment statement for the variables X and Y that uses the PUT function, the renamed numeric variables and the PUT function that uses the user-defined format that will convert the numeric variables to character and assign them as the new character variables for X and Y. Assuming you no longer want the numeri version of the variables, I used the DROP statement to remove them for the output data set WORK.SAMPLE2.

 

 

Here is a sample program that may help you with both examples:

/* Create User-Defined Format */
proc format;
  value Syndrome
    1="Weight gain"
    2="Other clinical signs"
  ;
run;

/* Create small WORK.TEST data set for the Examples*/
data test;
   x=1;
   y=2;
run;

/* Example 1 */
data sample1;
   set test;
   charx=put(x,Syndrome.);
   chary=put(y,Syndrome.);
run;

title1 "PROC CONTENTS OF WORK.SAMPLE1";
proc contents data=sample1;
run;

title1 "WORK.SAMPLE1 DATA";
proc print data=sample1;
run;

/* Example 2 */
data sample2;
   set test(rename=(x=numx y=numy));
   x=put(numx,Syndrome.);
   y=put(numy,Syndrome.);
   drop numx numy;
run;

title1 "PROC CONTENTS OF WORK.SAMPLE2";
proc contents data=sample2;
run;

title1 "WORK.SAMPLE2 DATA";
proc print data=sample2;
run;

title;

Hopefully this helps and with less lines of code than you would have to use if you used conditional logic to create the character version of the numeric variables.

 

For more information on the PUT function, take a look here.

 

Good Luck!

D_Dunlap

View solution in original post

9 REPLIES 9
Quentin
Super User

if you run:

proc contents data=have (keep=Syndrome_1 Syndrome_2);
run;

proc freq data=have;
  tables Syndrome_1 Syndrome_2 ;
  format Syndrome_1 Syndrome_2 ; *remove any formats attached to the variables;
run;

What do you see? 

 

It sounds like the variable type is numeric.  If that's the case typically you would have a format which maps each numeric code to a text string, something like:

proc format;
  value Syndrome
    1="Weight gain"
    2="Other clinical signs"
  ;
run;

Do you have a format like that?

The Boston Area SAS Users Group is hosting free webinars!

Register now at https://www.basug.org/events.
NewUsrStat
Lapis Lazuli | Level 10
Yes I have it. I thought there was a way to convert directly without specifying.
Tom
Super User Tom
Super User

Your code tried to convert the numbers in Syndrome_1 into an 8 digit string (right aligned).  So if the value was the number 1 (or 1.1 for that matter) then the result would have been seven spaces followed by the digit 1.

 

To use the PUT() function you would need to know what format to use that displays the value as you want them. But you did not tell us is what FORMAT is attached to the variable.  You should be able to see that information when you run PROC CONTENTS on the dataset.

 

But if you just want to get the formatted value of a variable you can do without knowing what format is being used by using the VVALUE() function.

Syndrome_char_1 = vvalue(Syndrome_1) ;
Syndrome_char_2 = vvalue(Syndrome_2) ;

 

NOTE: FORMAT has a very specific meaning in SAS and you are not using that noun in the same way in your post. In SAS a FORMAT is instructions for how to convert values into text.  You appear to be using the word format to refer to the TYPE of the variable.  

 

So your variable's type is numeric with a storage length of 8 bytes.  It has the INFORMAT of 8. attached to it, but what informat was used to read the values does not really matter that much once the values have been stored in the dataset.  But the key thing to understanding what value is stored to represent "Weight gain" is to know format that is attached to the variable. And the definition of that format.

 

 

 

 

dxiao2017
Lapis Lazuli | Level 10

Hi @NewUsrStat , I do not fully understand your question, so I take a guess as follows:

(1) You have a dataset that has a variable, the name of which is syndrome something. This variable is either a numeric variable (that has numbers as its values, such as 1 or 2) or a character variable(that has character strings as its values, such as weight gain or other clinical signs, or, syndrome_1 or syndrome_2), in this case, the type of your variable would NOT be numeric (if it is set as numeric, the value will be missing, i.e., the character string values could not be displayed for a numeric variable).

(2) You have a format (which is setup or written already) for use, this format converts your variable to the specific values, which in this case is: weight gain or other clinical signs. This format could be numeric format(which can be applied to a numeric variable) or character format(which can be applied to a character variable). And in this case, you do not need to use put() function to convert the variable values to what you want it to be, you only need to apply the format that has been already provided to you. One issue here is, after you applied the format, the type of the variable does NOT change, i.e., applying formats does NOT change the variable type (that is, if the variable is numeric, it remains as numeric type after the format is applied, if the variable is character, it remains as character type after the format is applied. Formats only change the way the values to be displayed, it does not change the variable type. 

(3) The above described datasets(with numeric or character variable), formats(that to be applied to the numeric or character variable), datasets with formats applied, and the codes for applying the formats are as follows. Hope this helps. (and btw, you said you have a list of variables in your DB, here DB means database, not something or columns or variables in your survey data, right?)

data have1;
   input subjid $ syndrome var1 var2;
   datalines;
A01 1 1 2
A02 1 3 12
A03 2 3 10
B01 1 2 20
B02 2 2 13
B03 2 1 21
;
run;
title "have1";
proc contents data=have1;run;
proc print data=have1;run;
data have2;
   input subjid $ syndrome $10. var1 var2;
   datalines;
A01 syndrome_1 1 2
A02 syndrome_1 3 12
A03 syndrome_2 3 10
B01 syndrome_1 2 20
B02 syndrome_2 2 13
B03 syndrome_2 1 21
;
run;
title "have2";
proc contents data=have2;run;
proc print data=have2;run;
proc format;
   value sdmfmtnum 1='weight gain'
                   2='other clinical signs';
run;

proc format;
   value $sdmfmtchar 'syndrome_1'='weight gain'
                     'syndrome_2'='other clinical signs';
run;
data want1;
   set have1;
   format syndrome sdmfmtnum.;
run;
title "want1";
proc contents data=want1;run;
proc print data=want1;run;
data want2;
   set have2;
   format syndrome $sdmfmtchar.;
run;
title "want2";
proc contents data=want2;run;
proc print data=want2;run;

dxiao2017_0-1770125585918.png

 

dxiao2017_1-1770125630237.png

 

dxiao2017_2-1770125680404.png

 

dxiao2017_3-1770125718007.png

 

PaigeMiller
Diamond | Level 26

@NewUsrStat wrote:

 

Can anyone help me please?


The best way to get help is to provide sample data as working SAS data step code (examples and instructions) and also provide the code you tried to use to convert the numeric to character.

 

Please provide the data following the instructions; please provide the code you used that didn't work.

--
Paige Miller
D_Dunlap
SAS Employee

Hello @NewUsrStat ,

If I'm understanding you correctly, the underlying data is numeric, but a user-defined format has been created to display the data as character, in which case, the underlying data (X and Y) are still stored as numeric, but the format is like icing on the cake, it makes the data prettier and more understandable. 

 

However, if you want to create a character version of the variables from the underlying table, this is possible. Since you already have a user-defined FORMAT, you can take advantage of that and use it in the PUT function to convert the numeric values to character values. 

 

There are two possible scenarios I can think of her in regard to converting the numeric values to characters.

 

Example 1: You want to create two new character variables with the different names from the original variables X and Y based on the user-defined format and store both the numeric form and character form of the variables in WORK.SAMPLE1.

 

Example 2: You want to convert X and Y to character variables and assign the character values base on the user-defined format. In which case, you have to RENAME X and Y initially because variables in SAS can't be both numeric and character at the same time. Then you can use an assignment statement for the variables X and Y that uses the PUT function, the renamed numeric variables and the PUT function that uses the user-defined format that will convert the numeric variables to character and assign them as the new character variables for X and Y. Assuming you no longer want the numeri version of the variables, I used the DROP statement to remove them for the output data set WORK.SAMPLE2.

 

 

Here is a sample program that may help you with both examples:

/* Create User-Defined Format */
proc format;
  value Syndrome
    1="Weight gain"
    2="Other clinical signs"
  ;
run;

/* Create small WORK.TEST data set for the Examples*/
data test;
   x=1;
   y=2;
run;

/* Example 1 */
data sample1;
   set test;
   charx=put(x,Syndrome.);
   chary=put(y,Syndrome.);
run;

title1 "PROC CONTENTS OF WORK.SAMPLE1";
proc contents data=sample1;
run;

title1 "WORK.SAMPLE1 DATA";
proc print data=sample1;
run;

/* Example 2 */
data sample2;
   set test(rename=(x=numx y=numy));
   x=put(numx,Syndrome.);
   y=put(numy,Syndrome.);
   drop numx numy;
run;

title1 "PROC CONTENTS OF WORK.SAMPLE2";
proc contents data=sample2;
run;

title1 "WORK.SAMPLE2 DATA";
proc print data=sample2;
run;

title;

Hopefully this helps and with less lines of code than you would have to use if you used conditional logic to create the character version of the numeric variables.

 

For more information on the PUT function, take a look here.

 

Good Luck!

D_Dunlap

dxiao2017
Lapis Lazuli | Level 10

Hi @NewUsrStat , I think @D_Dunlap is right about that you have a numeric variable that has already been applied a format which convert its numeric values 1 or 2 to syndrome_1 and syndrome_2, now you want to convert this variable to character variable that has weight gain and other clinical signs as its values. My steps to solve this question is as follows. I suppose dataset have2 is what you have now, and dataset want2 is what you want the variable to be. However in my final dataset, the variable name and position (it is the last variable in the dataset now) are not properly addressed. Just for your reference.

 

I use if/then; statement to change the values to character strings. An issues here is that, just like @Tom already mentioned, when you want to convert the value 1 and 2 to character strings, please pay attention to the length of the variable and use compress() function, otherwise SAS would not process the statement because the value here is actually 1 with several space ahead of it ( "        1"), not '1'(without any space ahead of the number).

data have1;
   input subjid $ syndrome var1 var2;
   datalines;
A01 1 1 2
A02 1 3 12
A03 2 3 10
B01 1 2 20
B02 2 2 13
B03 2 1 21
;
run;
title "have1";
proc contents data=have1;run;
proc print data=have1;run;
proc format;
   value sdmfmtnum 1='syndrome_1'
                   2='syndrome_2';
run;
data have2;
   set have1;
   format syndrome sdmfmtnum.;
run;
title "have2";
proc contents data=have2;run;
proc print data=have2;run;

data want1;
   set have2;
   syndromechar=put(syndrome,20.);
run;
title "want1";
proc contents data=want1;run;
proc print data=want1;run;

data want2;
   set want1;
   if compress(syndromechar)='1'
      then syndromechar='weight gain';
   else if compress(syndromechar)='2'
      then syndromechar='other clinical signs';
run;
title "want2";
proc contents data=want2;run;
proc print data=want2;run;

dxiao2017_0-1770210096818.png

 

dxiao2017_1-1770210149075.png

 

dxiao2017_2-1770210195421.png

 

dxiao2017_3-1770210253718.png

 

Tom
Super User Tom
Super User

Just don't put the leading spaces into the character variable to begin with and then there is no need to remove them later.

data want1;
   set have2;
   syndromechar=put(syndrome,20.-L);
run;

And if you want to remove leading spaces then use a function that just removes the leading spaces, not one that removes spaces anywhere in the string.

if left(syndromechar)='1'

 

dxiao2017
Lapis Lazuli | Level 10
Thanks a lot for mention this Tom! I forget compress() function will remove all spaces in a string, left() is the right one to use here.

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 1685 views
  • 11 likes
  • 6 in conversation