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

Hello, 

My data is imported having numeric variables ( values are 1 and 2) then I'm using data step to apply format values that are already defined in proc format ( proc format; value $myvalues 1='my first sentence' 2='my second sentence' ; ), so my variable should be character variable, while proc content from data step shows that it is numeric, therefore to convert the data to char I'm using put statement (char=put( numeric, 32.)) in second data step that I created, but proc print output shows 'char' is empty???!!! . Just mentioning that I'm using number 32 in put statement because the length of 'my first sentence' value defined in proc format is 32 character. Appreciate for any help.    

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Please post the code you have using the "insert SAS code" button.

The format-statement attaches a format to a variable only changing the way the values are displayed, the values stay unchanged. Maybe you just need something like

alphaVar = put(numVar, yourFormat.);

Please note that you should define the length of "alphaVar" to avoid information loss.

View solution in original post

5 REPLIES 5
andreas_lds
Jade | Level 19

Please post the code you have using the "insert SAS code" button.

The format-statement attaches a format to a variable only changing the way the values are displayed, the values stay unchanged. Maybe you just need something like

alphaVar = put(numVar, yourFormat.);

Please note that you should define the length of "alphaVar" to avoid information loss.

fatemeh
Quartz | Level 8

Thanks for your reply therefore How to permanently changing the variable's type (character or numeric ) in sas data set ?

andreas_lds
Jade | Level 19

@fatemeh wrote:

Thanks for your reply therefore How to permanently changing the variable's type (character or numeric ) in sas data set ?


You can't change the type of variable. You could create a new variable with the desired type and drop the old one.

ballardw
Super User

@fatemeh wrote:

Hello, 

My data is imported having numeric variables ( values are 1 and 2) then I'm using data step to apply format values that are already defined in proc format ( proc format; value $myvalues 1='my first sentence' 2='my second sentence' ; ), so my variable should be character variable, while proc content from data step shows that it is numeric, therefore to convert the data to char I'm using put statement (char=put( numeric, 32.)) in second data step that I created, but proc print output shows 'char' is empty???!!! . Just mentioning that I'm using number 32 in put statement because the length of 'my first sentence' value defined in proc format is 32 character. Appreciate for any help.    


apply format values that are already defined in proc format ( proc format; value $myvalues 1='my first sentence' 2='my second sentence' ; ), so my variable should be character variable

The above is a misunderstanding of what formats do. A format applied to a variable never changes the values at all. Formats change appearance. The function, such as PUT, uses the format to assign a value to a different variable.

If your format is truly named $myvalues then you should have a warning or error in the log about attempting to use a character format with a numeric variable. If the value is numeric the format name should be Myvalues and defined in Proc format with that name.

 

Without actual data and the complete code you have used "why" or what happened would be guesses.

A common issue with using the Variable = put(numeric, <some format>) is that by default numeric values would be right justified by the Put. Sometimes this can result in the "putted" value being to far to the right to fit. Additionally even if a variable has a Length of 32 it might have acquired a Format, such as $10., that displays fewer characters. So the right justified converted numeric value is just not displayed because it does not appear in the first 10 characters.

Sometimes it is very appropriate to want the "put" value to be left justified. That can be accomplished by adding a -L to the Put function:  Variable = put(value, 32. -L);

Here is brief example:

data junk;
   length char char2 $ 32 char3 $ 12;
   char = put(1, 32.);
   char2 = put(1, 32. -L);
   char3 = put(1, 32.);
run;

If you look at the data set Junk a table viewer you will see the one appears in different places in Char and Char2 and not at all in Char3.

Please note that most of the table generating procedures such as Proc Print , Freq and so on, will by default display the value left justified even though there are leading spaces. So a table viewer and not procedure is needed to see this specific behavior.

Depending on characteristics of your custom format, such as default length, the format definition can have an affect as well.

proc format;
value codea (default=8)
1 = 'my first sentence'
;
value codeb (default=24)
1 = 'my first sentence'
;
value codec
1 = 'my first sentence'
;

run;

data junk;
   x=1;
   char  = put(x, codea.);
   char2 = put(x, codeb.);
   char3 = put(x, codec.);
run;

 

 

 

fatemeh
Quartz | Level 8
Thanks for your reply. It was very helpful.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 5 replies
  • 2205 views
  • 3 likes
  • 3 in conversation