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

I am trying to create the variable year as below from _Name_. Below is the code. Any help, comes out incorrect.

 

 

data longMLS2;
set longMLS2 ;
year=input(substr(_NAME_, 4), 3.);
drop _name_;
run;

 

obs caseid _Name_ BMI Year
1 1 BMI_Y2 . 2
2 1 BMI_Y3 . 3
3 1 BMI_Y4 . 3
4 1 BMI_Y5 .  
5 1 BMI_Y6 .  
6 1 BMI_Y7 .  
7 1 BMI_Y8 .  
8 1 BMI_Y9 .  
9 1 BMI_Y10 19.3579  
10 1 BMI_Y11 23.1639  
11 1 BMI_Y12 23.7976  
12 1 BMI_Y13 24.7655  
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

HI @Reeza  You taught me anydigit exactly 19 months ago. 

 


data have;
input obs	caseid	_Name_	$ BMI;*	Year;
cards;
1	1	BMI_Y2	.	2
2	1	BMI_Y3	.	3
3	1	BMI_Y4	.	3
4	1	BMI_Y5	.	 
5	1	BMI_Y6	.	 
6	1	BMI_Y7	.	 
7	1	BMI_Y8	.	 
8	1	BMI_Y9	.	 
9	1	BMI_Y10	19.3579	 
10	1	BMI_Y11	23.1639	 
11	1	BMI_Y12	23.7976	 
12	1	BMI_Y13	24.7655	 
;

data want;
set have;
year=substr(_name_, anydigit(_Name_));
run;

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

substr(_NAME_, 4) applied to the first row of your data set produces the result _Y2. Do you see what is wrong now?

--
Paige Miller
desireatem
Pyrite | Level 9

Does not work for me.

Reeza
Super User
Use SCAN() since you have a clear delimiter.

Code = scan(_name_, 2, "_");


desireatem
Pyrite | Level 9

Thank you. It is now Y2, Y3.., how do I drop the Y so that I have 2, 3, ...

Reeza
Super User

Why not add Y to your SCAN() as a delimiter and you'll get just the number.

You may need to change 2 to 3 but it's worth trying first. 

Use INPUT() to convert it to a number, which will likely be your next question, so you can sort correctly. 

 


Code = input(scan(_name_, 2, "_Y"), 8.);

@desireatem wrote:

Thank you. It is now Y2, Y3.., how do I drop the Y so that I have 2, 3, ...


 

novinosrin
Tourmaline | Level 20

HI @Reeza  You taught me anydigit exactly 19 months ago. 

 


data have;
input obs	caseid	_Name_	$ BMI;*	Year;
cards;
1	1	BMI_Y2	.	2
2	1	BMI_Y3	.	3
3	1	BMI_Y4	.	3
4	1	BMI_Y5	.	 
5	1	BMI_Y6	.	 
6	1	BMI_Y7	.	 
7	1	BMI_Y8	.	 
8	1	BMI_Y9	.	 
9	1	BMI_Y10	19.3579	 
10	1	BMI_Y11	23.1639	 
11	1	BMI_Y12	23.7976	 
12	1	BMI_Y13	24.7655	 
;

data want;
set have;
year=substr(_name_, anydigit(_Name_));
run;
desireatem
Pyrite | Level 9

Thank you very much for your help!

Reeza
Super User
Yup, there's many ways to slice this one.
Another viable option is COMPRESS() which will keep only digits.

compress(_name_, , 'kd');
novinosrin
Tourmaline | Level 20

Reeza's special in my notes :

 

1. Proc freq sparse

2. Any...... group of functions with precisions

3. Not to trust anydate informats unless you know for real and sure

4. Remerge in SQL if system is fast enough and convenient

5. Informat statement better than a colon format modifier for reading ease 

 

and the list goes on to 143 points so far. I am definitely not sharing all. It's my selfish effort to take notes. lol But kudos for offering lol 🙂

novinosrin
Tourmaline | Level 20

Or Compress with 'kd' modifier

 


data have;
input obs	caseid	_Name_	$ BMI;*	Year;
cards;
1	1	BMI_Y2	.	2
2	1	BMI_Y3	.	3
3	1	BMI_Y4	.	3
4	1	BMI_Y5	.	 
5	1	BMI_Y6	.	 
6	1	BMI_Y7	.	 
7	1	BMI_Y8	.	 
8	1	BMI_Y9	.	 
9	1	BMI_Y10	19.3579	 
10	1	BMI_Y11	23.1639	 
11	1	BMI_Y12	23.7976	 
12	1	BMI_Y13	24.7655	 
;

data want;
set have;
year=compress(_name_,,'kd');
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1250 views
  • 8 likes
  • 4 in conversation