BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

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;
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this

 

Data final;
    set ABC1912 
        ABC2001  
        ABC2002  
        ABC2003 indsname=ds;
    newvar = compress(scan(ds,2,'.'),, 'kd');
Run;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Try this

 

Data final;
    set ABC1912 
        ABC2001  
        ABC2002  
        ABC2003 indsname=ds;
    newvar = compress(scan(ds,2,'.'),, 'kd');
Run;
Ronein
Onyx | Level 15

Can you please explain why you used number 2 ?

What is ds?

Does 'kd' means to take only digits?

compress(scan(ds,2,'.'),, 'kd');

 

Kurt_Bremser
Super User

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").

PaigeMiller
Diamond | Level 26
Data final;
set ABC1912 ABC2001  ABC2002  ABC2003 indsname=indsname;
source=substr(scan(indsname,2),4);
Run;
--
Paige Miller
Ronein
Onyx | Level 15

Great!

Now all is clear.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 2134 views
  • 2 likes
  • 4 in conversation