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

2 part questions, PLEASE HELP! 

 

I currently have 

 

01OCT2015:15:22:22 in my column.. I want to add a new column that will just say OCTOBER 

 

Im using SAS PROC SQL and need to creat another table.. 


So it would be rsubmit;

proc sql;

create table test1.new as 

.......

 

2)

I have a column that says CARS and and MONTHS ( that will be from the above query) 

One of the times I need to count the number of cars purchased for each months. 

and then i need another query that shows only the UNIQUE numbers of CARS for each months. 

 

So one with total (with dups) and other time with just Unique ones?

 

PLEASE HELP. Also using proc sql and need to create another table. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
majdi_ka
Obsidian | Level 7

 

Here is an example that can help. the first data step is to simulate the data that you have. The second is to create the output that you wanted.

 

 

data have;
format date datetime21.;
input date datetime21. ;
cards;
01OCT2015:15:22:22
3OCT2015:15:22:22
01NOV2015:15:22:22
01MAR2015:15:22:22
01FEB2015:15:22:22
01SEP2015:15:22:22
01AUG2015:15:22:22
;
run;

data want;
	set have;
	month=put(datepart(date),worddatx9.);
run;

 

 

 

View solution in original post

11 REPLIES 11
majdi_ka
Obsidian | Level 7

Hi,

 

1) Here's an example that you can execute

 

data _null_;
    month=put(datepart("01OCT2015:15:22:22"dt),worddatx9.);
    put month=;
run;

- 01OCT2015:15:22:22 is a datetime. To convert it to a date you can use datepart function.
- worddatw9. is a format that displays the date's month.

- put function creates a string based on the format applyed.

 

 

2) 

proc sql;
   create table want as
        select month
                 ,count(1) as nb_cars
                 ,count(distinct cars) as nb_distinct_cars
          from have
             group by month
;
quit;

 

Hope this helps !

 

itshere
Obsidian | Level 7

For the first part.. you did 

(datepart("01OCT2015:15:22:22"dt)

but its the entire column thats like that so I would need to convert the entire column.

 

Also.. how do i leave that column and create a new one with the actual MONTH?

 

Thank you!! 

majdi_ka
Obsidian | Level 7

 

Here is an example that can help. the first data step is to simulate the data that you have. The second is to create the output that you wanted.

 

 

data have;
format date datetime21.;
input date datetime21. ;
cards;
01OCT2015:15:22:22
3OCT2015:15:22:22
01NOV2015:15:22:22
01MAR2015:15:22:22
01FEB2015:15:22:22
01SEP2015:15:22:22
01AUG2015:15:22:22
;
run;

data want;
	set have;
	month=put(datepart(date),worddatx9.);
run;

 

 

 

itshere
Obsidian | Level 7

I used the second query and put in the column name in the "date"  and it didnt work.... 

 

how would i create a table rathe than overwrite the existing one?

majdi_ka
Obsidian | Level 7
Can you please share your code ?
itshere
Obsidian | Level 7
NOTE: Variable start_time is uninitialized.
month=.
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 at 17:15
majdi_ka
Obsidian | Level 7
This is the log, the result of the execution of your code, and not the code. Can you the code you executed that you find in the editor ?
itshere
Obsidian | Level 7
rsubmit;
data test1.new
month=put(datepart(start_time),worddatx9.);
put month=;
run;
majdi_ka
Obsidian | Level 7
you forgot the set statement with the table containing your variable start_time.
itshere
Obsidian | Level 7
but will it create a new column or overwrite the existing column?
DeepakSwain
Pyrite | Level 9

Hi itshere,

I have make a simple try below assuming different type of car purchased per month. If you could provide more information about uniqueness, I may try to refine it further.

data test1;
format date datetime21.;
input  type $ date datetime21.  ;
cards;
a 01OCT2015:15:22:22
b 3OCT2015:15:22:22
a 11OCT2015:15:22:22
b 13OCT2015:15:22:22
c 11OCT2015:15:22:22
b 30OCT2015:15:22:22
a 01NOV2015:15:22:22
a 02NOV2015:15:22:22
b 03NOV2015:15:22:22
a 01MAR2015:15:22:22
b 02MAR2015:15:22:22
c 03MAR2015:15:22:22
;
run;

data test2;
	set test1;
	month=put(datepart(date),worddatx9.);
run;

proc sql;
   create table test3 as
        select month, type,count(type) as num_of_car_per_month
          from test2
             group by month, type
;
quit;

 

Swain

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 11 replies
  • 2307 views
  • 2 likes
  • 3 in conversation