So, I'm currently working on SAS homework and I've come across an infuriating issue. I'm trying to convert a date (numeric data type with a permanently assigned DATE9. format) to a character data type so I can concat it with another character. So I'm taking a data set and using it to create a new temp data set. According to the professor, we need to use put or input and this is what the output should look like:
So the issue I'm having is when I use the put function to convert ship_date (Ship_Date = put(Ship_Date, DDMMYY10.); ) this is the output I get:
However, if I were to create a new variable from ship_date ( Ship_Date = put(Ship_Date, DDMMYY10.); ) I get the wanted output. This is driving me absolutely bonkers as I cannot figure it out. Here is my full SAS code:
data shipping_notes;
set orion.shipped;
Ship_Date = put(Ship_Date, DDMMYY10.);
Price = input(Price, dollar7.2);
length Comment $ 21.;
Comment = cat('Shipped on ', Ship_Date);
Total = Quantity * Price;
run;
proc print data=shipping_notes;
format Total dollar7.2;
run;
I know I have to be overthinking this, I just can't figure out what I'm doing wrong.
Maxim 2: Read the Log. You will find a NOTE for your attempt to assign a character value to the numeric variable ship_date.
Build your new variable in one step:
length comment $21;
comment = 'Shipped on ' !! put(ship_date,mmddyy10.);
Maxim 2: Read the Log. You will find a NOTE for your attempt to assign a character value to the numeric variable ship_date.
Build your new variable in one step:
length comment $21;
comment = 'Shipped on ' !! put(ship_date,mmddyy10.);
Thank you! This worked perfectly
Your log will look like this:
73 data want; 74 set have; 75 Ship_Date = put(Ship_Date, DDMMYY10.); 76 Price = input(Price, dollar7.2); 77 length Comment $ 21.; 78 Comment = cat('Shipped on ', Ship_Date); 79 Total = Quantity * Price; 80 run; NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 75:13 NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column). 76:15 NOTE: Invalid numeric data, '05/01/2011' , at Zeile 75 Spalte 13. product_id=240800200021 ship_date=. quantity=2 price=. Comment=Shipped on . Total=. _ERROR_=1 _N_=1 NOTE: Missing values were generated as a result of performing an operation on missing values. Each place is given by: (Number of times) at (Line):(Column). 1 bei 79:18
The first NOTE tells you that the result of the PUT function had to be converted to numeric. This NOT is created at compile time.
The second NOTE tells you that price is already numeric and had to be converted to a string so it could be used as argument for the INPUT function. This whole statement is not necessary. This NOTE was also created at compile time.
The next NOTE (which will be repeated several times for your ORION dataset) informs you that the result of the PUT function cannot be converted to numeric. This NOTE is created at runtime.
Your code should be:
data want;
set have;
length Comment $ 21.;
format Total dollar10.2;
Comment = cat('Shipped on ', put(ship_date,mmddyy10.));
Total = Quantity * Price;
run;
which gives you this clean log:
73 data want; 74 set have; 75 length Comment $ 21.; 76 format Total dollar10.2; 77 Comment = cat('Shipped on ', put(ship_date,mmddyy10.)); 78 Total = Quantity * Price; 79 run; NOTE: There were 1 observations read from the data set WORK.HAVE. NOTE: The data set WORK.WANT has 1 observations and 6 variables. NOTE: Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit): real time 0.00 seconds cpu time 0.01 seconds
and results in this dataset:
Beob. product_id ship_date quantity price Comment Total 1 240800200021 05JAN2011 2 $42.45 Shipped on 01/05/2011 $84.90
You cannot change the type of a variable. So trying to assign a character string to a numeric variable is not going to work well.
But there is no need to change SHIP_DATE to create COMMENT.
data shipping_notes;
set orion.shipped;
length Comment $21;
Comment = catx(' ','Shipped on',put(Ship_Date,yymmdd10.));
Total = Quantity * Price;
format total dollar7.2 ;
run;
PS Remind your instructor that using MDY or DMY ordering for dates will confuse half of your audience.
PPS Lengths are always integers so there is no need to include a decimal point when specifying the length of a variable.
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.
Ready to level-up your skills? Choose your own adventure.