BookmarkSubscribeRSS Feed
CathyVI
Pyrite | Level 9

Hi,

I am using the put function to change a numeric varible to character variable. Then I will like to rename this observations.

I have used this method for practice and it worked but when I applied it, it didnt work.This is my code.

The only observation that showed in my new table is the  "missing2".

 

proc sql;
create table pt_discstat as
select *,
case
when pt_discharge =  "-999" then "missing2"
when pt_discharge =  "12" then "homeH"
when pt_discharge =  "11" then "hospice"
when pt_discharge =  "10" then "otherD"
when pt_discharge =  "-9" then "missing"
when pt_discharge =  "-7" then "unknown"
when pt_discharge =  "-5" then "invalid"
when pt_discharge =  "9" then "otherH"
when pt_discharge =  "8" then "dead"
when pt_discharge =  "7" then "court"
when pt_discharge =  "6" then "long"
when pt_discharge =  "5" then "short"
when pt_discharge =  "4" then "LAMA"
when pt_discharge =  "3" then "admit"
when pt_discharge =  "1" then "home"
end as pt_recode
from pt_discharge_x
where pt_discharge in ("-999" "12" "11" "10" "-9" "-7" "-5" "9" "8" "7" "6" "5" "4" "3" "1");
quit;

9 REPLIES 9
mklangley
Lapis Lazuli | Level 10

Try putting commas in your IN statement:

where pt_discharge in ("-999", "12", "11", "10", "-9", "-7", "-5", "9", "8", "7", "6", "5", "4", "3", "1");
CathyVI
Pyrite | Level 9
Problem still persist
mklangley
Lapis Lazuli | Level 10

Is your question about the PUT function, as your title indicates? I don't see anything about that in your code. Either way, please include the relevant code and a sample of your input data and desired output data. Those are helpful to understand what "didnt work."

CathyVI
Pyrite | Level 9

Sorry about the confusion my question is specific to the sql, case-when procedure not the put statement.
This is a synthesis data.
data test;
input patient;
datalines;
-999
12
11
10
-9
-7
-5
9
8
7
6
5
4
3
1
;
run;
The data is numeric and I converted into character using this code:
data test2;
set test;
pt_discharge=put(patient,4.);
run;
Now I will like to rename the observations from -999 to 1 to character values. This is the outcome (table) I hope to achieve.

 

patient pt_discharge
-999 missing2
12 homeH
11  hospice
10 otherD
-9missing
-7 unknown
-5invalid
9  otherH
8  dead
7  court
6  long
5  short
4  LAMA
3 admit
1home

 

andreas_lds
Jade | Level 19

No need to convert the values to alphanumeric, just use a format:

proc format;
    value recode 
        -999 = "missing2" 
        12 = "homeH" 
        11 = "hospice" 
        10 = "otherD"
        -9 = "missing"
        -7 = "unknown"
        -5 = "invalid" 
        9 = "otherH" 
        8 = "dead" 
        7 = "court" 
        6 = "long" 
        5 = "short" 
        4 = "LAMA" 
        3 = "admit" 
        1 = "home"
   ;
run;

data want;
    set test;
    length pt_discharge $ 10;
    pt_discharge = put(patient, recode.);
run;
ballardw
Super User

@CathyVI wrote:

Sorry about the confusion my question is specific to the sql, case-when procedure not the put statement.
This is a synthesis data.
data test;
input patient;
datalines;
-999
12
11
10
-9
-7
-5
9
8
7
6
5
4
3
1
;
run;
The data is numeric and I converted into character using this code:
data test2;
set test;
pt_discharge=put(patient,4.);
run;
Now I will like to rename the observations from -999 to 1 to character values. This is the outcome (table) I hope to achieve.

 

patient  pt_discharge
-999  missing2
12  homeH
11   hospice
10  otherD
-9 missing
-7  unknown
-5 invalid
9   otherH
8   dead
7   court
6   long
5   short
4   LAMA
3  admit
1 home

 


This

pt_discharge=put(patient,4.);

creates values that have leading blank characters for any value that can be displayed with 3,2 or 1 character.

So your "=" does not match these created text values.

Use

pt_discharge=put(patient,4. -L);

to left justify the resulting text.

 

You also need to be aware that most of the output will strip the leading space for display an you just don't see

r_behata
Barite | Level 11

IMO, changing pt_discharge from numeric to character is not necessary here.

You may recode this using Formats.

 

Untested code :

 

proc format;
	value $ recode 
		-999 = "missing2"
		12 = "homeH"
		11 = "hospice"
		10 = "otherD"
		-9 = "missing"
		-7 = "unknown"
		-5 = "invalid"
		9 = "otherH"
		8 = "dead"
		7 = "court"
		6 = "long"
		5 = "short"
		4 = "LAMA"
		3 = "admit"
		1 = "home"
	;
run;

proc sql;
	create table pt_discstat as
		select *,
			 put(pt_discharge,$recode.) as	 pt_recode
			from pt_discharge_x
				where pt_discharge in (-999, 12, 11 ,10, -9 ,-7 ,-5, 9, 8, 7, 6, 5, 4, 3, 1);
quit;

 

ballardw
Super User

@CathyVI wrote:

Hi,

I am using the put function to change a numeric varible to character variable. Then I will like to rename this observations.

I have used this method for practice and it worked but when I applied it, it didnt work.This is my code.

The only observation that showed in my new table is the  "missing2".

 

proc sql;
create table pt_discstat as
select *,
case
when pt_discharge =  "-999" then "missing2"
when pt_discharge =  "12" then "homeH"
when pt_discharge =  "11" then "hospice"
when pt_discharge =  "10" then "otherD"
when pt_discharge =  "-9" then "missing"
when pt_discharge =  "-7" then "unknown"
when pt_discharge =  "-5" then "invalid"
when pt_discharge =  "9" then "otherH"
when pt_discharge =  "8" then "dead"
when pt_discharge =  "7" then "court"
when pt_discharge =  "6" then "long"
when pt_discharge =  "5" then "short"
when pt_discharge =  "4" then "LAMA"
when pt_discharge =  "3" then "admit"
when pt_discharge =  "1" then "home"
end as pt_recode
from pt_discharge_x
where pt_discharge in ("-999" "12" "11" "10" "-9" "-7" "-5" "9" "8" "7" "6" "5" "4" "3" "1");
quit;


What does your PT_discharge_x data set look like?

 

If these values are the result of your "put" to create character variables you likely need to share the code. The output of Put for a numeric value is usually right justified and so the results would have sign-ficant leading spaces that would not compare to most of your values. "   1" is not the same as "1".

 

Example run this code and look at the results for the Char values.

data example;
   input x;
   char1 = Quote(put(x,f4.));
   char2 = quote(put(x,f4. -L));

datalines;
-999
12
11
10
-9
-7
;

The -L on the Put function left justifies results and likely is what you need. Trailing blanks as part of the value are ignored generally for comparison.

 

Tom
Super User Tom
Super User

That is because it is the only value that matches the list in your WHERE clause.

 

If you use something like PUT(xxx,4.) to make PT_DISCHARGE then the values are right aligned (have leading spaces).  You can use the -L modifier on your format specification to have them left aligned: PUT(xxx,4.-L) 

 

To work with what you have try removing the leading spaces.

where left(pt_discharge) in ("-999" "12" "11" "10" "-9" "-7" "-5" "9" "8" "7" "6" "5" "4" "3" "1");

Then you should get more matches.

 

Of course you also need to adjust the WHEN conditions.  You could use LEFT() there also to remove the leading spaces.  Or if you know how many leading spaces there are just include them in the comparison strings.

when pt_discharge =  "-999" then "missing2"
when pt_discharge =  "  12" then "homeH"

 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 9 replies
  • 1728 views
  • 0 likes
  • 6 in conversation