<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Extract a subset of a numerical variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608808#M177210</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285961"&gt;@jpprovost&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can't specify 201910 as a date constant, because a date constant must incude a value for the day as shown here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;44   data _null_;
45       ym = '201910'd;
              ---------
              77
ERROR: Invalid date/time/datetime constant '201910'd.
ERROR 77-185: Invalid number conversion on '201910'd.

46   run;

NOTE: The SAS System stopped processing this step because of errors.
...&lt;BR /&gt;
47
48   data _null_;
49       ym = '01oct2019'd;
50   run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;if you have 201910 as a string or want to specify it as a constant, it is necessary to use an input function with the informat yymmn6. The format yymmn6. works both as informat and format, so you can go from the string 201910 to a SAS date value representing 01oct2019 and from the SAS date value back to the string 201910 as shown here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	format ym ddmmyyd10.;
	ym = input('201910',yymmn6.);
	put ym=;

	str_ym = put(ym,yymmn6.);
	put str_ym=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The result is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ym=01-10-2019&lt;BR /&gt;str_ym=201910&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards&lt;/P&gt;
&lt;P&gt;Erik&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Ok, I understand.&lt;/P&gt;
&lt;P&gt;So if I use ym = '01OCT2019'd is there a way to extract the string "OCT2019" from the variable?&lt;/P&gt;</description>
    <pubDate>Mon, 02 Dec 2019 18:21:17 GMT</pubDate>
    <dc:creator>jpprovost</dc:creator>
    <dc:date>2019-12-02T18:21:17Z</dc:date>
    <item>
      <title>Extract a subset of a numerical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608360#M177039</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am struggling (and I don't know why) to extract the first 4 digits and the last two digits of a variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's the variable :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let ym = 201910;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I tried this command and this is not working. I don't know what I do wrong...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
test1 = substr(&amp;amp;ym., 1, 4);
test2 = substr(&amp;amp;ym., 4, 2);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe it's because we're Friday...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help would be very appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Fri, 29 Nov 2019 20:49:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608360#M177039</guid>
      <dc:creator>jpprovost</dc:creator>
      <dc:date>2019-11-29T20:49:47Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a subset of a numerical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608361#M177040</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%let ym = 201910;


 

data want;
test1 = substr("&amp;amp;ym.", 1, 4);
test2 = substr("&amp;amp;ym.", 5, 2);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Nov 2019 20:52:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608361#M177040</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-11-29T20:52:45Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a subset of a numerical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608363#M177042</link>
      <description>&lt;P&gt;SUBSTR() works on character strings. But SAS will happily convert your number 201,910 into a character string for you.&amp;nbsp; The problem is it does that using the BEST12. format which will right align the result. So since your value only needs 6 digits the first 6 characters are all spaces.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Convert your number to a character string.&amp;nbsp; That is easy to do in SAS when working with constants by just putting quotes around the digits. Examples:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;test1 = substr("201910",1,4);
test1 = substr("&amp;amp;ym",1,4);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really have a numeric variable you can use the PUT() to convert it to a character string in the right format. Examples:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;test1=substr(put(X,6.),1,4);
test1=substr(left(put(X,20.)),1,4);
test1=substr(put(X,20.-L),1.4);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or if you want numbers as the result then just use arithmetic. Examples:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;year=int(201910/100);
month=mod(201910,100);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Nov 2019 21:05:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608363#M177042</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-29T21:05:10Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a subset of a numerical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608380#M177051</link>
      <description>&lt;P&gt;Another option is to use the &lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p0wqoixcwtx2jqn1ovqf06kart0o.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#p1ft41nwszevabn1dkor5ammqm5x" target="_blank" rel="noopener"&gt;SUBSTRN function&lt;/A&gt; instead of the SUBSTR function, as I've learned from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;only &lt;A href="https://communities.sas.com/t5/New-SAS-User/how-to-get-the-value-digit-in-a-lot-of-value-numbers-in-sas-eg/m-p/606498#M17415" target="_blank" rel="noopener"&gt;last week&lt;/A&gt;:&lt;/P&gt;
&lt;PRE&gt;test1 = substr&lt;STRONG&gt;n&lt;/STRONG&gt;(&amp;amp;ym., 1, 4);
test2 = substr&lt;STRONG&gt;n&lt;/STRONG&gt;(&amp;amp;ym., 5, 2);&lt;/PRE&gt;
&lt;P&gt;(I had always thought the "N" in the name stands just for "&lt;EM&gt;n&lt;/EM&gt;ull string," but it can also be a reminder of "&lt;EM&gt;n&lt;/EM&gt;umeric arguments allowed.")&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result is still a character string, though. There's also a pitfall if you haven't defined the length of the character variable receiving the result: "&lt;SPAN&gt;that variable is given the length of the first argument," says the documentation, but this means length &lt;STRONG&gt;8&lt;/STRONG&gt; for &lt;EM&gt;any&lt;/EM&gt; numeric constant and &amp;lt;=8 for a numeric variable as the first argument,&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;regardless&lt;/EM&gt;&amp;nbsp;of the length specified in the third argument.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Nov 2019 23:11:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608380#M177051</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-11-29T23:11:22Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a subset of a numerical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608421#M177073</link>
      <description>&lt;P&gt;Yikes.&amp;nbsp; That is going to cause confusion if used with with %SYSFUNC().&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How will it know whether the input string is to be treated as a number or a string?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1811  %let str=123E4 ;
1812  %put str=|&amp;amp;str| substr=|%substr(&amp;amp;str,1)| substrn=|%sysfunc(substrn(&amp;amp;str,1))|;
str=|123E4| substr=|123E4| substrn=|1230000|&lt;/PRE&gt;
&lt;P&gt;I will probably stick with my macro version instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro substrn
/*----------------------------------------------------------------------
Subset string (simulation of SUBSTRN function)
----------------------------------------------------------------------*/
(string   /* String to subset */
,position /* Start position */
,length   /* Length of substring */
);
%local slen start end;
%*----------------------------------------------------------------------
Get length of string. Calculate start and end positions within string.
----------------------------------------------------------------------;
%let slen=%length(&amp;amp;string);
%let start=%sysfunc(max(&amp;amp;position,1));
%if ^%length(&amp;amp;length) %then %let end=&amp;amp;slen;
%else %let end=%sysfunc(min(&amp;amp;slen,&amp;amp;length+&amp;amp;position-1));

%*---------------------------------------------------------------------
Use SUBSTR to return part of string requested.
----------------------------------------------------------------------;
%if (&amp;amp;start &amp;lt;= &amp;amp;slen) and (&amp;amp;start &amp;lt;= &amp;amp;end) %then
  %substr(&amp;amp;string,&amp;amp;start,&amp;amp;end-&amp;amp;start+1)
;

%mend substrn;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 30 Nov 2019 07:03:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608421#M177073</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-30T07:03:03Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a subset of a numerical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608430#M177075</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Yikes.&amp;nbsp; That is going to cause confusion if used with with %SYSFUNC().&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How will it know whether the input string is to be treated as a number or a string?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Agreed. Interesting that it seems to prioritize the numeric interpretation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Quote from the &lt;A href="https://documentation.sas.com/#p1lsz837462hpjn12ki1a34914m2" target="_blank" rel="noopener"&gt;documentation&lt;/A&gt;:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;"&lt;SPAN&gt;If you call SUBSTRN by using the %SYSFUNC macro, then the macro processor resolves the first argument (&lt;/SPAN&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;string&lt;/EM&gt;&lt;SPAN&gt;) to determine whether the argument is character or numeric. If you do not want the first argument to be evaluated as a macro expression, use one of the macro-quoting functions in the first argument.&lt;/SPAN&gt;"&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;However, none of the macro quoting functions (applied in the definition of STR or in the first argument of SUBSTRN or a combination of both) prevents that numeric evaluation and hence the result 1230000.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's what comes of softening the distinction between numeric and character functions. (Same with CAT, etc.).&lt;/P&gt;</description>
      <pubDate>Sat, 30 Nov 2019 11:07:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608430#M177075</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-11-30T11:07:33Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a subset of a numerical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608470#M177092</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285961"&gt;@jpprovost&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your macro variable &lt;STRONG&gt;&lt;EM&gt;ym&lt;/EM&gt;&lt;/STRONG&gt; seems to contain a year-month value, and it might be a better idea to convert the value to a SAS date value and use the provided SAS functions to extract the year and month.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I always prefer to use SAS date and time values instead of numeric literals, because they can be presented with many different formats, and it is so easy to manipulate them with SAS functions working on date and time values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Examples:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let ym = 201910;
data _null_; 
	date = input("&amp;amp;ym",yymmn6.);
	year = year(date);
	month = month(date);
	put year= / month=;
run;&lt;BR /&gt;

%let ym = 201910;
%let date = %sysfunc(inputn(&amp;amp;ym,yymmn6.));
data _null_; 
	year = year(&amp;amp;date);
	month = month(&amp;amp;date);
	put year= / month=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Both gives the same result:&lt;/P&gt;
&lt;P&gt;year=2019&lt;BR /&gt;month=10&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This gives the possibility to make computations, e.g. compute the 6. month after without problems, when the result is in another year:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let ym = 201910;
data _null_; 
	ym = input("&amp;amp;ym",yymmn6.);
	half_year_after = intnx('month',ym,6);
	put ym= yymmn6./ half_year_after= yymmn6.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;result:&lt;/P&gt;
&lt;P&gt;ym=201910&lt;BR /&gt;half_year_after=202004&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards&lt;/P&gt;
&lt;P&gt;Erik&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Nov 2019 18:24:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608470#M177092</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-11-30T18:24:22Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a subset of a numerical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608724#M177182</link>
      <description>&lt;P&gt;Every solution seems to work, I will accept yours because the &amp;amp;ym macro is effectively a month-year variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a subquestion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's say that :&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;ym = '201910'd&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to extract the string "201910" from this variable with this particular format?&lt;/P&gt;</description>
      <pubDate>Mon, 02 Dec 2019 15:09:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608724#M177182</guid>
      <dc:creator>jpprovost</dc:creator>
      <dc:date>2019-12-02T15:09:47Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a subset of a numerical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608768#M177198</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285961"&gt;@jpprovost&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can't specify 201910 as a date constant, because a date constant must incude a value for the day as shown here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;44   data _null_;
45       ym = '201910'd;
              ---------
              77
ERROR: Invalid date/time/datetime constant '201910'd.
ERROR 77-185: Invalid number conversion on '201910'd.

46   run;

NOTE: The SAS System stopped processing this step because of errors.
...&lt;BR /&gt;
47
48   data _null_;
49       ym = '01oct2019'd;
50   run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;if you have 201910 as a string or want to specify it as a constant, it is necessary to use an input function with the informat yymmn6. The format yymmn6. works both as informat and format, so you can go from the string 201910 to a SAS date value representing 01oct2019 and from the SAS date value back to the string 201910 as shown here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	format ym ddmmyyd10.;
	ym = input('201910',yymmn6.);
	put ym=;

	str_ym = put(ym,yymmn6.);
	put str_ym=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The result is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ym=01-10-2019&lt;BR /&gt;str_ym=201910&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards&lt;/P&gt;
&lt;P&gt;Erik&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Dec 2019 16:40:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608768#M177198</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-12-02T16:40:46Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a subset of a numerical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608783#M177201</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285961"&gt;@jpprovost&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Every solution seems to work, I will accept yours because the &amp;amp;ym macro is effectively a month-year variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a subquestion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's say that :&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;ym = '201910'd&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there a way to extract the string "201910" from this variable with this particular format?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is a poor format to use since it kind of looks like a date literal, but it isn't. Data literals require that the quoted string can be read by the DATE informat.&lt;/P&gt;
&lt;P&gt;You could use %SCAN() to remove the stuff between the quotes.&lt;/P&gt;
&lt;PRE&gt;2315  %let ym = '201910'd;
2316  %let ym2 = %scan(&amp;amp;ym,1,'');
2317  %put &amp;amp;=ym &amp;amp;=ym2;
YM='201910'd YM2=201910
&lt;/PRE&gt;</description>
      <pubDate>Mon, 02 Dec 2019 17:11:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608783#M177201</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-02T17:11:55Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a subset of a numerical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608808#M177210</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285961"&gt;@jpprovost&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can't specify 201910 as a date constant, because a date constant must incude a value for the day as shown here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;44   data _null_;
45       ym = '201910'd;
              ---------
              77
ERROR: Invalid date/time/datetime constant '201910'd.
ERROR 77-185: Invalid number conversion on '201910'd.

46   run;

NOTE: The SAS System stopped processing this step because of errors.
...&lt;BR /&gt;
47
48   data _null_;
49       ym = '01oct2019'd;
50   run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;if you have 201910 as a string or want to specify it as a constant, it is necessary to use an input function with the informat yymmn6. The format yymmn6. works both as informat and format, so you can go from the string 201910 to a SAS date value representing 01oct2019 and from the SAS date value back to the string 201910 as shown here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	format ym ddmmyyd10.;
	ym = input('201910',yymmn6.);
	put ym=;

	str_ym = put(ym,yymmn6.);
	put str_ym=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The result is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ym=01-10-2019&lt;BR /&gt;str_ym=201910&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards&lt;/P&gt;
&lt;P&gt;Erik&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Ok, I understand.&lt;/P&gt;
&lt;P&gt;So if I use ym = '01OCT2019'd is there a way to extract the string "OCT2019" from the variable?&lt;/P&gt;</description>
      <pubDate>Mon, 02 Dec 2019 18:21:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608808#M177210</guid>
      <dc:creator>jpprovost</dc:creator>
      <dc:date>2019-12-02T18:21:17Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a subset of a numerical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608829#M177225</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285961"&gt;@jpprovost&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Oh yes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The date literal '01oct2019'd gives a SAS date value, which is the number&amp;nbsp;of days since jan 1, 1960.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The number can be presented in any possible way of writing a date by applying a format. It is still a number, but it is shown or printed as specified by the format, see below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The number can also be translated to a string value containing any of these formats with the put function, where the number is "written" to a character variable using the format. This is how we get 01OCT2019 from the number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The character variable can be manipulated with SAS string functions. A substring from the 3. character gives OCT2019.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	date = '01oct2019'd;
	put 'Internal representation as number:   ' date;
	put 'Still a number, but shown formatted: ' date yymmn6. ' ' date mmddyy10. ' ' date date9.;
	cdate = put(date,date9.);
	put 'Converted to string variable:        ' date;
	cdate = substr(put(date,date9.),3);
	put 'Substring of string variable:        ' cdate;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Internal representation as number: 21823&lt;BR /&gt;Still a number, but shown formatted: 201910 10/01/2019 01OCT2019&lt;BR /&gt;Converted to string variable: 01OCT2019&lt;BR /&gt;Substring of string variable: OCT2019&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards&lt;/P&gt;
&lt;P&gt;Erik&lt;/P&gt;</description>
      <pubDate>Mon, 02 Dec 2019 19:41:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608829#M177225</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-12-02T19:41:07Z</dc:date>
    </item>
    <item>
      <title>Re: Extract a subset of a numerical variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608830#M177226</link>
      <description>&lt;P&gt;There is a format that will display the data in that form.&lt;/P&gt;
&lt;PRE&gt;2318  %let ym = '10-OCT-2019'd;
2319  %let ym2 = %sysfunc(putn(&amp;amp;ym,monyy7));
2320  %put &amp;amp;=ym &amp;amp;=ym2;
YM='10-OCT-2019'd YM2=OCT2019
&lt;/PRE&gt;</description>
      <pubDate>Mon, 02 Dec 2019 19:45:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-a-subset-of-a-numerical-variable/m-p/608830#M177226</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-02T19:45:12Z</dc:date>
    </item>
  </channel>
</rss>

