BookmarkSubscribeRSS Feed
beekee
Calcite | Level 5

Hi, I am new to using SAS Studio and I am not sure how I can keep the “year” variable as a 4-digit year value by using “Year4.” as its format? Currently the "year" in my dataset is numeric (e.g. 2020, 2021, 2022).

 

Below is my current code:

data Test.tourism;
infile "/home/u59111097/TMA/tourism.csv" dlm=',' FIRSTOBS = 2;
input year vsp pdi puk exuk pop cpisp exsp;
put tourism format year year4.;
run;

 

Thank you.

3 REPLIES 3
PaigeMiller
Diamond | Level 26

YEAR of 2020, for example, does not need to be formatted as YEAR4. It already appears to us humans as the integer 2020, formatting would not improve the appearance.

--
Paige Miller
Reeza
Super User
Formats are applied to SAS date variables, which require a day, month and year component. You could convert your year to a SAS date but there isn't huge value to that unless you're planning to do analysis by month or days.

SAS_year = mdy(1, 1, year);
format SAS_year year4.;

Tom
Super User Tom
Super User

If the CSV file has 4 digits for YEAR and you read it using your example INPUT statement then the values are just being stored as numbers not date values.  SAS stores dates as the number of DAYS since 1960, not the number of YEARS since 0 which what it looks like your code is assuming the CSV has.

 

So there is no need to attach any format at all to YEAR.  SAS will display integers such as 2,020 and 2,021 as four digits strings like 2020 and 2021 without any need for you to give it special instructions on how to display the values.  If you did want to attach a format then probably 4. would be good although if you want them to display with the thousands separator you might use comma5. instead.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1605 views
  • 0 likes
  • 4 in conversation