hello
I am using set statement to concatenate data sets.
I want to create a new column with information of source data set (value will be from letter 4 till letter 7 in data set name. for example: if source data set is ABC1912 then value will be 1912 and so on
Data ABC1912;
input x;
cards;
1
2
;
run;
Data ABC2001;
input x;
cards;
11
22
;
run;
Data ABC2002;
input x;
cards;
13
22
;
run;
Data ABC2003;
input x;
cards;
1
23
;
run;
Data final;
set ABC1912 ABC2001 ABC2002 ABC2003;
/*I want to create a new variable that will get value from the source data set name:
If the observation from data set ABC1912 then will get value 1912
If the observation from data set ABC2001 then will get value 2001
If the observation from data set ABC2002 then will get value 2002
If the observation from data set ABC2003 then will get value 2003*/
Run;
Try this
Data final;
set ABC1912
ABC2001
ABC2002
ABC2003 indsname=ds;
newvar = compress(scan(ds,2,'.'),, 'kd');
Run;
Try this
Data final;
set ABC1912
ABC2001
ABC2002
ABC2003 indsname=ds;
newvar = compress(scan(ds,2,'.'),, 'kd');
Run;
Can you please explain why you used number 2 ?
What is ds?
Does 'kd' means to take only digits?
compress(scan(ds,2,'.'),, 'kd');
The dataset name as returned into the INDSNAME= variable contains the library and dataset name, so the SCAN function extracts the dataset name. The COMPRESS then extracts the part you are interested in, as you suspected (see section "modifier").
Data final;
set ABC1912 ABC2001 ABC2002 ABC2003 indsname=indsname;
source=substr(scan(indsname,2),4);
Run;
Great!
Now all is clear.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.