Hi,
If I have a dataset with a CHAR variable named YR that has values in YYYYMM, like 202101 to 202112 and I wanted to modify this dataset so that the YYYY will be 2023, 202301 to 202312. Also, this dataset is indexed.
I used the following code:
proc sql;
update mydata
set substr(YR, 1, 4) = '2023';
quit;For some reason, I keep getting an error of Expecting =.
Question 1:
What's the proper way to use the substr function in PROC SQL?
Question 2:
What is the correct method for modify this variable in a DATA step without losing the index?
Thanks!
One example with actual data values
data have;
input YR $;
datalines;
199901
199902
202109
;
proc sql;
update have
set yr= cats('2023',substr(yr,5));
quit;
Why: From documentation from Proc SQL
Syntax
UPDATE table-name | sas/access-view | proc-sql-view <AS alias>SET column-1=sql-expression-1 <, column-2=sql-expression-2, ...>
<SET column-1=sql-expression-1 <, column-1=sql-expression-2, ...>><WHERE sql-expression>;
Set COLUMN= not Set <some function call>= . You need to use the variable name as the target of the SET.
As for changing the value of a variable without changing the index think of what an INDEX is: as short cut for finding specific values. If you want your data set to be indexed in file order add another variable and make that the index. If you want to use this variable then reindex on the new values.
Now for the maybe more important part: WHY is a date value in a character variable? You lose all of the date functions and have to do gyrations with odd code like this.
One example with actual data values
data have;
input YR $;
datalines;
199901
199902
202109
;
proc sql;
update have
set yr= cats('2023',substr(yr,5));
quit;
Why: From documentation from Proc SQL
Syntax
UPDATE table-name | sas/access-view | proc-sql-view <AS alias>SET column-1=sql-expression-1 <, column-2=sql-expression-2, ...>
<SET column-1=sql-expression-1 <, column-1=sql-expression-2, ...>><WHERE sql-expression>;
Set COLUMN= not Set <some function call>= . You need to use the variable name as the target of the SET.
As for changing the value of a variable without changing the index think of what an INDEX is: as short cut for finding specific values. If you want your data set to be indexed in file order add another variable and make that the index. If you want to use this variable then reindex on the new values.
Now for the maybe more important part: WHY is a date value in a character variable? You lose all of the date functions and have to do gyrations with odd code like this.
To update data in place look at the MODIFY statement.
To make a NEW dataset with the same index(es) then just specify the index(es) in the DATA statement.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.