SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aleksi555
Fluorite | Level 6

I have a proc sql query containing year(date)=2018 in the where-clause and everything works normally. However, I needed to add a year(date) variable to select-clause in character format so I tried to add "put(year(date),16.)"  but didnt get any results. I automatically started to search error in my where-condition but surprisingly didnt find any and confirmed that this new variable in select-clause causes the zero results (no error messages but only no results). Does anyone know why this happens? This doesnt happen in SQLServer. There seems to be connection between the year(date) variable in select-clause and where-clause although shouldnt ( I removed the year(date) from where-clause and got results again). Please see example below:

 

Works:

select id, year(date) from data1

where year(date)=2018

 

Doesnt yield any results nor error messages:

select id,put(year(date),16.) from data1

where year(date)=2018

1 ACCEPTED SOLUTION

Accepted Solutions
KentaMURANAKA
Pyrite | Level 9

Hi,

 

I still don't understand your problem well, but you want to convert numeric variable to characteristic variable in SQL, right??

Your format "16." in PUT function is what I don't know well. Anyway, how about this?

data test;
    date=today(); year=year(date); output;
    date=today()-100;  year=year(date); output;
    date=today()-1000; year=year(date); output;
run;
proc sql;
    create table work.test_1 as
    select *, put(year, best.)
    from work.test
    where year eq 2018
    ;
quit;

View solution in original post

2 REPLIES 2
KentaMURANAKA
Pyrite | Level 9

Hi,

 

I still don't understand your problem well, but you want to convert numeric variable to characteristic variable in SQL, right??

Your format "16." in PUT function is what I don't know well. Anyway, how about this?

data test;
    date=today(); year=year(date); output;
    date=today()-100;  year=year(date); output;
    date=today()-1000; year=year(date); output;
run;
proc sql;
    create table work.test_1 as
    select *, put(year, best.)
    from work.test
    where year eq 2018
    ;
quit;
ballardw
Super User

@aleksi555 wrote:

I have a proc sql query containing year(date)=2018 in the where-clause and everything works normally. However, I needed to add a year(date) variable to select-clause in character format so I tried to add "put(year(date),16.)"  but didnt get any results. I automatically started to search error in my where-condition but surprisingly didnt find any and confirmed that this new variable in select-clause causes the zero results (no error messages but only no results). Does anyone know why this happens? This doesnt happen in SQLServer. There seems to be connection between the year(date) variable in select-clause and where-clause although shouldnt ( I removed the year(date) from where-clause and got results again). Please see example below:

 

Works:

select id, year(date) from data1

where year(date)=2018

 

Doesnt yield any results nor error messages:

select id,put(year(date),16.) from data1

where year(date)=2018


You did not give SQL the Name of the resulting value: put(year(date),16.) as YearChar

or what ever the name of the result should be. I would question using 16 characters though.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 2 replies
  • 2790 views
  • 1 like
  • 3 in conversation