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.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.
Find more tutorials on the SAS Users YouTube channel.