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

Hello , I wish to create a variable "M" that extract the observations marked "bold" from days

 

Obs

days

1

20150320

2

20140609

3

20140807

4

20140828

5

20150418

6

20150602

7

20150625

8

20141029

9

20150220

10

20150407

 

Thank you!!!

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

Hoping that the days variable is a character variable

 

data want;
set have;
m=substr(strip(days),5,2);
run;
Thanks,
Jag

View solution in original post

5 REPLIES 5
Jagadishkatam
Amethyst | Level 16

Hoping that the days variable is a character variable

 

data want;
set have;
m=substr(strip(days),5,2);
run;
Thanks,
Jag
desireatem
Pyrite | Level 9

Thank you!!!!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Whilst @Jagadishkatam is quite correct, that with the example you have given in a string variable, you can substr out various parts, I would also point out two things.  Firstly that is a date field, and you would be best off treating it as a date.  Second as that is a string field you could have anything in it, I have seen:

YY0401 or 120405

The substr would not work in these cases, unless of course you do various checks before hand.  So when working with dates I always have the two components, the string value for display purposes to show it as entered, and a numeric version for processing.  You could for instance do:

mnth=month(input(days,anydtdte.));

Though why you would call a date variable days is beyond me.

desireatem
Pyrite | Level 9

who cares what name I give to my variables. I just wanted to extract some number from it and some showed me, what is your issue with it.

desireatem
Pyrite | Level 9
This is it, working well, what is your issue with trying to be coolest:

data have;
input obs days;
cards;

1 20150320
2 20140609
3 20140807
4 20140828
5 20150418
;
run;
data want;
set have;
m=substr(strip(days),5,2);
run;


proc print data=want;
run;
Obs obs days m
1 1 20150320 03
2 2 20140609 06
3 3 20140807 08
4 4 20140828 08
5 5 20150418 04


SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1048 views
  • 0 likes
  • 3 in conversation